fix: correctly handle pathnames found in the _redirects file#12821
fix: correctly handle pathnames found in the _redirects file#12821
_redirects file#12821Conversation
🦋 Changeset detectedLatest commit: f99eead The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
I see this method is building a |
|
preview: https://svelte-dev-git-preview-kit-12821-svelte.vercel.app/ this is an automated message |
|
Thanks, that's a good suggestion. I've added a link to the _routes.json documentation. |
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
dummdidumm
left a comment
There was a problem hiding this comment.
Is this still all correct now that there was a breaking change about the other things that no longer are in the static directory?
documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Thanks for the reminder. I did a bit of refactoring and added a test for the I've also added a check in case someone added their The |
| /** @type {string[]} */ | ||
| let redirects = []; | ||
| if (existsSync(redirects_dest)) { | ||
| const redirect_rules = readFileSync(redirects_dest, 'utf8'); |
There was a problem hiding this comment.
what's this doing? It looks like it's reading redirects created during a previous build and reusing them? why would we want to do that?
There was a problem hiding this comment.
This part is reading the _redirects file we've created during the current build from combining the user's redirects with our generated ones. We need to read all of them so that we can add them to the _routes.json file's exclude array to prevent the worker from taking over where it shouldn't.
I got a bit confused myself when I read this code block. I think I should add a comment explaining these bits now that I remember.
EDIT: added a comment here https://github.com/sveltejs/kit/pull/12821/files#diff-78806028d48ad47894445421562b7796c53178948c017e13d4550def4193903eR138-R140
|
@teemingc is this currently ready for review? |
|
Yes, this one's good to go |
| /** | ||
| * We use a custom `Builder` type here to ensure compatibility with the minimum version of SvelteKit. | ||
| */ | ||
| type Builder2_0_0 = PartialExcept< |
There was a problem hiding this comment.
thanks i hate it
| const content = line.trim(); | ||
| if (!content) continue; |
There was a problem hiding this comment.
If you wanted to be clever you could .map(l => l.trim()).filter(Boolean) 😉
There was a problem hiding this comment.
I kinda like it but I tried it and I don't think it's too much better because of the increased iterations.
const redirects = file_contents
.split('\n')
.map((line) => line.trim())
.filter(Boolean)
.map((line) => {
const [pathname] = line.split(' ');
// pathnames with placeholders are not supported
if (!pathname || pathname.includes('/:')) {
throw new Error(
`The following _redirects rule cannot be excluded by _routes.json: ${line}`
);
}
return pathname;
});vs
/** @type {string[]} */
const redirects = [];
for (const line of file_contents.split('\n')) {
const content = line.trim();
if (!content) continue;
const [pathname] = line.split(' ');
// pathnames with placeholders are not supported
if (!pathname || pathname.includes('/:')) {
throw new Error(`The following _redirects rule cannot be excluded by _routes.json: ${line}`);
}
redirects.push(pathname);
}This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @sveltejs/adapter-netlify@6.0.0 ### Major Changes - breaking: `platform.context` is now the [modern Netlify Functions (\[#15203\](#15203)) context](https://docs.netlify.com/build/functions/api/#netlify-specific-context-object) Previously, this was the [AWS Lambda-style context](https://github.com/netlify/primitives/blob/c1ae30f2745f0a73e26e83334695e205a04ab47d/packages/functions/prod/src/function/handler_context.ts). If you were using this in your app (unlikely), you will need to update your code to read from new fields. ### Minor Changes - feat: Migrate to the modern Netlify Functions API ([#15203](#15203)) The Netlify adapter now generates "v2" Netlify Functions, which uses modern standards (ESM, `Request`, `Response`) instead of the legacy "Lambda-compatible" or "v1" format. Under the hood, this greatly simplifies the adapter code and improves maintainability. For more details on features this unlocks for your SvelteKit app, see <https://developers.netlify.com/guides/migrating-to-the-modern-netlify-functions/>. - feat: allow configuring redirects in `netlify.toml` ([#15203](#15203)) The limitation of only being able to configure redirects via the `_redirects` file has been removed. ### Patch Changes - fix: populate `App.Platform` with `context` property ([#15203](#15203)) - Updated dependencies \[[`37293a5`](37293a5), [`5d05ca6`](5d05ca6), [`ed69b77`](ed69b77), [`b1fc959`](b1fc959), [`159aece`](159aece), [`c690579`](c690579), [`dc8cf2d`](dc8cf2d), [`ace2116`](ace2116), [`0f38f49`](0f38f49)]: - @sveltejs/kit@2.51.0 ## @sveltejs/kit@2.51.0 ### Minor Changes - feat: add `scroll` property to `NavigationTarget` in navigation callbacks ([#15248](#15248)) Navigation callbacks (`beforeNavigate`, `onNavigate`, and `afterNavigate`) now include scroll position information via the `scroll` property on `from` and `to` targets: - `from.scroll`: The scroll position at the moment navigation was triggered - `to.scroll`: In `beforeNavigate` and `onNavigate`, this is populated for `popstate` navigations (back/forward) with the scroll position that will be restored, and `null` for other navigation types. In `afterNavigate`, this is always the final scroll position after navigation completed. This enables use cases like animating transitions based on the target scroll position when using browser back/forward navigation. - feat: `hydratable`'s injected script now works with CSP ([#15048](#15048)) ### Patch Changes - fix: put preloads before styles ([#15232](#15232)) - fix: suppress false-positive inner content warning when children prop is forwarded to a child component ([#15269](#15269)) - fix: `fetch` not working when URL is same host but different than `paths.base` ([#15291](#15291)) - fix: navigate to hash link when base element is present ([#15236](#15236)) - fix: avoid triggering `handleError` when redirecting in a remote function ([#15222](#15222)) - fix: include `test` directory in generated `tsconfig.json` alongside existing `tests` entry ([#15254](#15254)) - fix: generate `tsconfig.json` using the value of `kit.files.src` ([#15253](#15253)) ## @sveltejs/adapter-cloudflare@7.2.7 ### Patch Changes - fix: error if `_routes.json` is in the `/static` public directory ([#12821](#12821)) - fix: correctly handle pathnames found in the `_redirects` file ([#12821](#12821)) - Updated dependencies \[[`37293a5`](37293a5), [`5d05ca6`](5d05ca6), [`ed69b77`](ed69b77), [`b1fc959`](b1fc959), [`159aece`](159aece), [`c690579`](c690579), [`dc8cf2d`](dc8cf2d), [`ace2116`](ace2116), [`0f38f49`](0f38f49)]: - @sveltejs/kit@2.51.0 ## @sveltejs/adapter-node@5.5.3 ### Patch Changes - fix: validate `ORIGIN` env var at startup ([#15045](#15045)) - chore(deps): update dependency `@rollup/plugin-commonjs` to v29 ([#14856](#14856)) - Updated dependencies \[[`37293a5`](37293a5), [`5d05ca6`](5d05ca6), [`ed69b77`](ed69b77), [`b1fc959`](b1fc959), [`159aece`](159aece), [`c690579`](c690579), [`dc8cf2d`](dc8cf2d), [`ace2116`](ace2116), [`0f38f49`](0f38f49)]: - @sveltejs/kit@2.51.0 ## @sveltejs/enhanced-img@0.10.1 ### Patch Changes - fix: replace erroneous `import.meta.DEV` with `import.meta.env.DEV` in generated code ([#15285](#15285)) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ejs#12821) * read redirects * documentation * add link * changeset * add link to expected JSON format * Update packages/adapter-cloudflare/index.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * extract to function * add test * better error message * Update documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> * fix lint * refactor * changeset * add test and fix rule truncation * fix lint * fix type error * fix lock file * explain redirect parsing * clarify * format --------- Co-authored-by: Chew Tee Ming <chew.tee.ming@nindatech.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @sveltejs/adapter-netlify@6.0.0 ### Major Changes - breaking: `platform.context` is now the [modern Netlify Functions (\[sveltejs#15203\](sveltejs#15203)) context](https://docs.netlify.com/build/functions/api/#netlify-specific-context-object) Previously, this was the [AWS Lambda-style context](https://github.com/netlify/primitives/blob/c1ae30f2745f0a73e26e83334695e205a04ab47d/packages/functions/prod/src/function/handler_context.ts). If you were using this in your app (unlikely), you will need to update your code to read from new fields. ### Minor Changes - feat: Migrate to the modern Netlify Functions API ([sveltejs#15203](sveltejs#15203)) The Netlify adapter now generates "v2" Netlify Functions, which uses modern standards (ESM, `Request`, `Response`) instead of the legacy "Lambda-compatible" or "v1" format. Under the hood, this greatly simplifies the adapter code and improves maintainability. For more details on features this unlocks for your SvelteKit app, see <https://developers.netlify.com/guides/migrating-to-the-modern-netlify-functions/>. - feat: allow configuring redirects in `netlify.toml` ([sveltejs#15203](sveltejs#15203)) The limitation of only being able to configure redirects via the `_redirects` file has been removed. ### Patch Changes - fix: populate `App.Platform` with `context` property ([sveltejs#15203](sveltejs#15203)) - Updated dependencies \[[`37293a5`](sveltejs@37293a5), [`5d05ca6`](sveltejs@5d05ca6), [`ed69b77`](sveltejs@ed69b77), [`b1fc959`](sveltejs@b1fc959), [`159aece`](sveltejs@159aece), [`c690579`](sveltejs@c690579), [`dc8cf2d`](sveltejs@dc8cf2d), [`ace2116`](sveltejs@ace2116), [`0f38f49`](sveltejs@0f38f49)]: - @sveltejs/kit@2.51.0 ## @sveltejs/kit@2.51.0 ### Minor Changes - feat: add `scroll` property to `NavigationTarget` in navigation callbacks ([sveltejs#15248](sveltejs#15248)) Navigation callbacks (`beforeNavigate`, `onNavigate`, and `afterNavigate`) now include scroll position information via the `scroll` property on `from` and `to` targets: - `from.scroll`: The scroll position at the moment navigation was triggered - `to.scroll`: In `beforeNavigate` and `onNavigate`, this is populated for `popstate` navigations (back/forward) with the scroll position that will be restored, and `null` for other navigation types. In `afterNavigate`, this is always the final scroll position after navigation completed. This enables use cases like animating transitions based on the target scroll position when using browser back/forward navigation. - feat: `hydratable`'s injected script now works with CSP ([sveltejs#15048](sveltejs#15048)) ### Patch Changes - fix: put preloads before styles ([sveltejs#15232](sveltejs#15232)) - fix: suppress false-positive inner content warning when children prop is forwarded to a child component ([sveltejs#15269](sveltejs#15269)) - fix: `fetch` not working when URL is same host but different than `paths.base` ([sveltejs#15291](sveltejs#15291)) - fix: navigate to hash link when base element is present ([sveltejs#15236](sveltejs#15236)) - fix: avoid triggering `handleError` when redirecting in a remote function ([sveltejs#15222](sveltejs#15222)) - fix: include `test` directory in generated `tsconfig.json` alongside existing `tests` entry ([sveltejs#15254](sveltejs#15254)) - fix: generate `tsconfig.json` using the value of `kit.files.src` ([sveltejs#15253](sveltejs#15253)) ## @sveltejs/adapter-cloudflare@7.2.7 ### Patch Changes - fix: error if `_routes.json` is in the `/static` public directory ([sveltejs#12821](sveltejs#12821)) - fix: correctly handle pathnames found in the `_redirects` file ([sveltejs#12821](sveltejs#12821)) - Updated dependencies \[[`37293a5`](sveltejs@37293a5), [`5d05ca6`](sveltejs@5d05ca6), [`ed69b77`](sveltejs@ed69b77), [`b1fc959`](sveltejs@b1fc959), [`159aece`](sveltejs@159aece), [`c690579`](sveltejs@c690579), [`dc8cf2d`](sveltejs@dc8cf2d), [`ace2116`](sveltejs@ace2116), [`0f38f49`](sveltejs@0f38f49)]: - @sveltejs/kit@2.51.0 ## @sveltejs/adapter-node@5.5.3 ### Patch Changes - fix: validate `ORIGIN` env var at startup ([sveltejs#15045](sveltejs#15045)) - chore(deps): update dependency `@rollup/plugin-commonjs` to v29 ([sveltejs#14856](sveltejs#14856)) - Updated dependencies \[[`37293a5`](sveltejs@37293a5), [`5d05ca6`](sveltejs@5d05ca6), [`ed69b77`](sveltejs@ed69b77), [`b1fc959`](sveltejs@b1fc959), [`159aece`](sveltejs@159aece), [`c690579`](sveltejs@c690579), [`dc8cf2d`](sveltejs@dc8cf2d), [`ace2116`](sveltejs@ace2116), [`0f38f49`](sveltejs@0f38f49)]: - @sveltejs/kit@2.51.0 ## @sveltejs/enhanced-img@0.10.1 ### Patch Changes - fix: replace erroneous `import.meta.DEV` with `import.meta.env.DEV` in generated code ([sveltejs#15285](sveltejs#15285)) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
closes #9138
We need to add the pathnames found in the Cloudflare
_redirectsfile to theexcludearray so that they are handled by Cloudflare Pages rather than the Cloudflare Pages worker. This PR also adds the<redirects>directive so that it can be included manually in theexcludearray in_routes.json.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits