Fix/netlify edge redirects (#3342)
* fix: create redirects file for netlify edge adapter * chore: changeset * chore: distinguish between edge/regular
This commit is contained in:
parent
9c98ba81e1
commit
352fc3166f
4 changed files with 40 additions and 21 deletions
5
.changeset/eighty-keys-explode.md
Normal file
5
.changeset/eighty-keys-explode.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/netlify': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
create redirects file for netlify edge adapter
|
|
@ -1,4 +1,5 @@
|
||||||
import type { AstroAdapter, AstroIntegration, AstroConfig, RouteData } from 'astro';
|
import type { AstroAdapter, AstroIntegration, AstroConfig, RouteData } from 'astro';
|
||||||
|
import { createRedirects } from './shared.js';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
|
||||||
export function getAdapter(): AstroAdapter {
|
export function getAdapter(): AstroAdapter {
|
||||||
|
@ -97,6 +98,7 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {})
|
||||||
},
|
},
|
||||||
'astro:build:done': async ({ routes, dir }) => {
|
'astro:build:done': async ({ routes, dir }) => {
|
||||||
await createEdgeManifest(routes, entryFile, _config.root);
|
await createEdgeManifest(routes, entryFile, _config.root);
|
||||||
|
await createRedirects(routes, dir, entryFile, true);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro';
|
import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro';
|
||||||
import fs from 'fs';
|
import { createRedirects } from './shared.js';
|
||||||
|
|
||||||
export function getAdapter(): AstroAdapter {
|
export function getAdapter(): AstroAdapter {
|
||||||
return {
|
return {
|
||||||
|
@ -37,26 +37,7 @@ function netlifyFunctions({ dist }: NetlifyFunctionsOptions = {}): AstroIntegrat
|
||||||
buildConfig.server = new URL('./functions/', _config.outDir);
|
buildConfig.server = new URL('./functions/', _config.outDir);
|
||||||
},
|
},
|
||||||
'astro:build:done': async ({ routes, dir }) => {
|
'astro:build:done': async ({ routes, dir }) => {
|
||||||
const _redirectsURL = new URL('./_redirects', dir);
|
await createRedirects(routes, dir, entryFile, false);
|
||||||
|
|
||||||
// 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');
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
31
packages/integrations/netlify/src/shared.ts
Normal file
31
packages/integrations/netlify/src/shared.ts
Normal file
|
@ -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');
|
||||||
|
}
|
Loading…
Reference in a new issue