0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00
astro/packages/integrations/netlify/src/shared.ts
Nate Moore d2960984c5
Experimental Prerender API (#5297)
* wip: hybrid output

* wip: hybrid output mvp

* refactor: move hybrid => server

* wip: hybrid support for `output: 'server'`

* feat(hybrid): overwrite static files

* fix: update static build

* feat(hybrid): skip page generation if no static entrypoints

* feat: migrate from hybrid output => prerender flag

* fix: appease typescript

* fix: appease typescript

* fix: appease typescript

* fix: appease typescript

* fix: improve static cleanup

* attempt: avoid preprocess scanning

* hack: force generated .js files to be treated as ESM

* better handling for astro metadata

* fix: update scanner plugin

* fix: page name bug

* fix: keep ssr false when generating pages

* fix: force output to be treated as ESM

* fix: client output should respect buildConfig

* fix: ensure outDir is always created

* fix: do not replace files with noop

* fix(netlify): add support for `experimental_prerender` pages

* feat: switch to `experimental_prerender`

* chore: update es-module-lexer code in test

* feat: improved code-splitting, cleanup

* feat: move prerender behind flag

* test: prerender

* test: update prerender test

* chore: update lockfile

* fix: only match `.html` files when resolving assets

* chore: update changeset

* chore: remove ESM hack

* chore: allow `--experimental-prerender` flag, move `--experimental-error-overlay` into subobject

* chore: update changeset

* test(vite-plugin-scanner): add proper unit tests for vite-plugin-scanner

* chore: remove leftover code

* chore: add comment on cleanup task

* refactor: move manual chunks logic to vite-plugin-prerender

* fix: do not support let declarations

* test: add var test

* refactor: prefer existing util

* Update packages/astro/src/@types/astro.ts

Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>

* Update packages/astro/src/core/errors/errors-data.ts

Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>

* Update packages/astro/src/@types/astro.ts

Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>

Co-authored-by: Nate Moore <nate@astro.build>
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
2022-12-16 10:38:37 -06:00

46 lines
1.4 KiB
TypeScript

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');
}