|
| 1 | +import url from "node:url"; |
| 2 | +import type { AstroConfig, AstroIntegrationLogger } from "astro"; |
| 3 | +import path from "node:path"; |
| 4 | +import fs from "node:fs/promises"; |
| 5 | + |
| 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 = ""; |
| 21 | + |
| 22 | +export async function configurePlugin(hookOptions: any) { |
| 23 | + const buildOutput: string = hookOptions.buildOutput; |
| 24 | + const config: AstroConfig = hookOptions.config; |
| 25 | + const logger: AstroIntegrationLogger = hookOptions.logger; |
| 26 | + |
| 27 | + if (buildOutput !== "static") { |
| 28 | + logger.warn( |
| 29 | + `Skip generating static redirects: not compatible with '${buildOutput}' builds, only 'static' is supported.`, |
| 30 | + ); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Find redirects |
| 35 | + const redirects = config.redirects; |
| 36 | + if (!Object.keys(redirects).length) { |
| 37 | + logger.warn("Skip generating static redirects: no redirects found."); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Add redirects |
| 42 | + logger.info("Generating static redirects file..."); |
| 43 | + Object.keys(redirects).forEach((from) => { |
| 44 | + const redirect = redirects[from]; |
| 45 | + |
| 46 | + if (typeof redirect === "string") { |
| 47 | + nginxRedirectRules += createNginxRule(from, redirect); |
| 48 | + } else { |
| 49 | + nginxRedirectRules += createNginxRule(from, redirect.destination); |
| 50 | + } |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +export async function writeToOutput(hookOptions: any) { |
| 55 | + const outDir: string = hookOptions.dir; |
| 56 | + const logger: AstroIntegrationLogger = hookOptions.logger; |
| 57 | + |
| 58 | + if (!nginxRedirectRules || !nginxRedirectRules.length) { |
| 59 | + logger.warn( |
| 60 | + `Skip generating static redirects file: no redirects were generated.`, |
| 61 | + ); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Write redirect.conf |
| 66 | + const configDestinationPath = path.join( |
| 67 | + url.fileURLToPath(outDir), |
| 68 | + "redirect.conf", |
| 69 | + ); |
| 70 | + await fs.writeFile(configDestinationPath, nginxRedirectRules); |
| 71 | + logger.info(`Generated static redirects file: ${configDestinationPath}`); |
| 72 | +} |
| 73 | + |
| 74 | +export default function staticRedirects() { |
| 75 | + return { |
| 76 | + name: "static-redirects", |
| 77 | + hooks: { |
| 78 | + "astro:config:done": async (hookOptions: any) => |
| 79 | + await configurePlugin(hookOptions), |
| 80 | + "astro:build:done": async (hookOptions: any) => |
| 81 | + await writeToOutput(hookOptions), |
| 82 | + }, |
| 83 | + }; |
| 84 | +} |
0 commit comments