diff options
Diffstat (limited to 'packages/integrations/cloudflare/src')
-rw-r--r-- | packages/integrations/cloudflare/src/index.ts | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 2f6b36e87..ca755432e 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -1,4 +1,5 @@ import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro'; +import { createRedirectsFromAstroRoutes, type Redirects } from '@astrojs/underscore-redirects'; import esbuild from 'esbuild'; import * as fs from 'fs'; import * as os from 'os'; @@ -50,6 +51,7 @@ export default function createIntegration(args?: Options): AstroIntegration { client: new URL(`.${config.base}`, config.outDir), server: new URL(`.${SERVER_BUILD_FOLDER}`, config.outDir), serverEntry: '_worker.mjs', + redirects: false, }, }); }, @@ -88,7 +90,7 @@ export default function createIntegration(args?: Options): AstroIntegration { vite.ssr.target = 'webworker'; } }, - 'astro:build:done': async ({ pages }) => { + 'astro:build:done': async ({ pages, routes, dir }) => { const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)); const entryUrl = new URL(_buildConfig.serverEntry, _config.outDir); const buildPath = fileURLToPath(entryUrl); @@ -197,6 +199,19 @@ export default function createIntegration(args?: Options): AstroIntegration { } } + const redirectRoutes = routes.filter(r => r.type === 'redirect'); + const trueRedirects = createRedirectsFromAstroRoutes({ + config: _config, + routes: redirectRoutes, + dir, + }); + if(!trueRedirects.empty()) { + await fs.promises.appendFile( + new URL('./_redirects', _config.outDir), + trueRedirects.print() + ); + } + await fs.promises.writeFile( new URL('./_routes.json', _config.outDir), JSON.stringify( |