@@ -3,7 +3,21 @@ import type { AstroConfig, AstroIntegrationLogger } from "astro";
33import path from "node:path" ;
44import fs from "node:fs/promises" ;
55
6- let configJson = { routes : [ ] } ;
6+ function createNginxRule ( redirectFrom : string , redirectTo : string ) {
7+ if ( redirectFrom . endsWith ( "/" ) ) {
8+ redirectFrom = redirectFrom . slice ( 0 , - 1 ) ;
9+ }
10+
11+ return (
12+ "rewrite ^" +
13+ redirectFrom +
14+ "(/?)$ $scheme://$http_host" +
15+ redirectTo +
16+ "/ permanent;\n"
17+ ) ;
18+ }
19+
20+ let nginxRedirectRules = "" ;
721
822export async function configurePlugin ( hookOptions : any ) {
923 const buildOutput : string = hookOptions . buildOutput ;
@@ -12,61 +26,27 @@ export async function configurePlugin(hookOptions: any) {
1226
1327 if ( buildOutput !== "static" ) {
1428 logger . warn (
15- `Skip generating static redirects file : not compatible with '${ buildOutput } ' builds, only 'static' is supported.` ,
29+ `Skip generating static redirects: not compatible with '${ buildOutput } ' builds, only 'static' is supported.` ,
1630 ) ;
1731 return ;
1832 }
1933
2034 // Find redirects
2135 const redirects = config . redirects ;
2236 if ( ! Object . keys ( redirects ) . length ) {
23- logger . warn ( "Skip generating static redirects file : no redirects found." ) ;
37+ logger . warn ( "Skip generating static redirects: no redirects found." ) ;
2438 return ;
2539 }
2640
27- // Load existing staticwebapp.config.json
28- const configSourcePath = path . join (
29- url . fileURLToPath ( config . srcDir ) ,
30- "staticwebapp.config.json" ,
31- ) ;
32-
33- try {
34- configJson = JSON . parse (
35- await fs . readFile ( configSourcePath , {
36- encoding : "utf-8" ,
37- } ) ,
38- ) ;
39-
40- if ( ! configJson . routes ) {
41- configJson . routes = [ ] ;
42- }
43- } catch {
44- logger . debug (
45- `Skip load existing config file: '${ configSourcePath } ' not found.` ,
46- ) ;
47- }
48-
4941 // Add redirects
5042 logger . info ( "Generating static redirects file..." ) ;
5143 Object . keys ( redirects ) . forEach ( ( from ) => {
5244 const redirect = redirects [ from ] ;
5345
5446 if ( typeof redirect === "string" ) {
55- // @ts -ignore
56- configJson . routes . push ( {
57- route : from ,
58- methods : [ "GET" ] ,
59- redirect : redirect ,
60- statusCode : 301 ,
61- } ) ;
47+ nginxRedirectRules += createNginxRule ( from , redirect ) ;
6248 } else {
63- // @ts -ignore
64- configJson . routes . push ( {
65- route : from ,
66- methods : [ "GET" ] ,
67- redirect : redirect . destination ,
68- statusCode : redirect . status ,
69- } ) ;
49+ nginxRedirectRules += createNginxRule ( from , redirect . destination ) ;
7050 }
7151 } ) ;
7252}
@@ -75,25 +55,20 @@ export async function writeToOutput(hookOptions: any) {
7555 const outDir : string = hookOptions . dir ;
7656 const logger : AstroIntegrationLogger = hookOptions . logger ;
7757
78- if ( ! configJson || ! configJson . routes . length ) {
58+ if ( ! nginxRedirectRules || ! nginxRedirectRules . length ) {
7959 logger . warn (
8060 `Skip generating static redirects file: no redirects were generated.` ,
8161 ) ;
8262 return ;
8363 }
8464
85- // Write staticwebapp.config.json
65+ // Write redirect.conf
8666 const configDestinationPath = path . join (
8767 url . fileURLToPath ( outDir ) ,
88- "staticwebapp.config.json" ,
89- ) ;
90- await fs . writeFile (
91- configDestinationPath ,
92- JSON . stringify ( configJson , null , 2 ) ,
93- ) ;
94- logger . info (
95- `Generated static redirects file: ${ configDestinationPath } (${ configJson . routes . length } redirects)` ,
68+ "redirect.conf" ,
9669 ) ;
70+ await fs . writeFile ( configDestinationPath , nginxRedirectRules ) ;
71+ logger . info ( `Generated static redirects file: ${ configDestinationPath } ` ) ;
9772}
9873
9974export default function staticRedirects ( ) {
0 commit comments