-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: create redirects file for netlify edge adapter * chore: changeset * chore: distinguish between edge/regular
- Loading branch information
Showing
4 changed files
with
40 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/netlify': patch | ||
--- | ||
|
||
create redirects file for netlify edge adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} |