From 352fc3166fe3b3d3da3feff621394b20eacb9cc3 Mon Sep 17 00:00:00 2001 From: Pascal Schilp Date: Wed, 11 May 2022 17:13:23 +0200 Subject: [PATCH] Fix/netlify edge redirects (#3342) * fix: create redirects file for netlify edge adapter * chore: changeset * chore: distinguish between edge/regular --- .changeset/eighty-keys-explode.md | 5 +++ .../netlify/src/integration-edge-functions.ts | 2 ++ .../netlify/src/integration-functions.ts | 23 ++------------ packages/integrations/netlify/src/shared.ts | 31 +++++++++++++++++++ 4 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 .changeset/eighty-keys-explode.md create mode 100644 packages/integrations/netlify/src/shared.ts diff --git a/.changeset/eighty-keys-explode.md b/.changeset/eighty-keys-explode.md new file mode 100644 index 000000000..abd3707f6 --- /dev/null +++ b/.changeset/eighty-keys-explode.md @@ -0,0 +1,5 @@ +--- +'@astrojs/netlify': patch +--- + +create redirects file for netlify edge adapter diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index 08835bd26..0f28aeab5 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -1,4 +1,5 @@ import type { AstroAdapter, AstroIntegration, AstroConfig, RouteData } from 'astro'; +import { createRedirects } from './shared.js'; import * as fs from 'fs'; export function getAdapter(): AstroAdapter { @@ -97,6 +98,7 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) }, 'astro:build:done': async ({ routes, dir }) => { await createEdgeManifest(routes, entryFile, _config.root); + await createRedirects(routes, dir, entryFile, true); }, }, }; diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index 2720eb591..81cb7e9fd 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -1,5 +1,5 @@ import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro'; -import fs from 'fs'; +import { createRedirects } from './shared.js'; export function getAdapter(): AstroAdapter { return { @@ -37,26 +37,7 @@ function netlifyFunctions({ dist }: NetlifyFunctionsOptions = {}): AstroIntegrat buildConfig.server = new URL('./functions/', _config.outDir); }, 'astro:build:done': async ({ routes, dir }) => { - const _redirectsURL = new URL('./_redirects', dir); - - // Create the redirects file that is used for routing. - let _redirects = ''; - for (const route of routes) { - if (route.pathname) { - _redirects += ` -${route.pathname} /.netlify/functions/${entryFile} 200`; - } else { - const pattern = - '/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/'); - _redirects += ` -${pattern} /.netlify/functions/${entryFile} 200`; - } - } - - // Always use appendFile() because the redirects file could already exist, - // e.g. due to a `/public/_redirects` file that got copied to the output dir. - // If the file does not exist yet, appendFile() automatically creates it. - await fs.promises.appendFile(_redirectsURL, _redirects, 'utf-8'); + await createRedirects(routes, dir, entryFile, false); }, }, }; diff --git a/packages/integrations/netlify/src/shared.ts b/packages/integrations/netlify/src/shared.ts new file mode 100644 index 000000000..3df667f4d --- /dev/null +++ b/packages/integrations/netlify/src/shared.ts @@ -0,0 +1,31 @@ +import type { RouteData } from 'astro'; +import fs from 'fs'; + +export async function createRedirects( + routes: RouteData[], + dir: URL, + entryFile: string, + edge: boolean + ) { + const _redirectsURL = new URL('./_redirects', dir); + const kind = edge ? 'edge-functions' : 'functions' + + // Create the redirects file that is used for routing. + let _redirects = ''; + for (const route of routes) { + if (route.pathname) { + _redirects += ` + ${route.pathname} /.netlify/${kind}/${entryFile} 200`; + } else { + const pattern = + '/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/'); + _redirects += ` + ${pattern} /.netlify/${kind}/${entryFile} 200`; + } + } + + // Always use appendFile() because the redirects file could already exist, + // e.g. due to a `/public/_redirects` file that got copied to the output dir. + // If the file does not exist yet, appendFile() automatically creates it. + await fs.promises.appendFile(_redirectsURL, _redirects, 'utf-8'); +} \ No newline at end of file