blob: 2c648984a2b6d13971570c5cb8cd24ef6f519830 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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) {
if (route.distURL) {
_redirects += `
${route.pathname} /${route.distURL.toString().replace(dir.toString(), '')} 200`;
} else {
_redirects += `
${route.pathname} /.netlify/${kind}/${entryFile} 200`;
if (route.route === '/404') {
_redirects += `
/* /.netlify/${kind}/${entryFile} 404`;
}
}
} else {
const pattern =
'/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/');
if (route.distURL) {
_redirects += `
${pattern} /${route.distURL.toString().replace(dir.toString(), '')} 200`;
} else {
_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');
}
|