-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adapter-cloudflare ignores _redirects
config for cloudflare pages
#9138
Comments
Are the docs wrong when they say to place this file in |
No, the docs are correct. @nosovk may I know what your use case is for this? |
We have site, which is hosted on Cloudflare pages. Currently we do something like that:
import { error, redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
const redirects = {
'/actions': '/what-we-do',
'/actions/200-days-into-russias-war-on-ukraine': '/whats-new/200-days-into-russias-war-on-ukraine',
// ...
/// ...
};
export const load: PageServerLoad = async ({ url }) => {
if (redirects[url.pathname]) {
throw redirect(301, redirects[url.pathname]);
}
throw error(404, 'Not found');
}; and it actually works when we have our categories renamed, like actions => whats-new But it's really good idea to manage all those redirects in one place, especially if there is some solution from platform. |
Thank you for sharing. You've actually helped correct my understanding about Cloudflare Pages redirects (previously I thought the redirect would bypass the worker, thus fail when arriving at the destination). I'd love to try my hand at implementing this. Although, one thing to be wary of is the 100 rule limit for Should the build warn or fail when the |
I would like to add that you cannot add all of your redirects to _routes.json. _routes.json has a limit of 100 rules. Unless many of those redirects could be handled under wildcards |
I think that prerendered pages and static assets should be level 1 citizen in routes, and warning user about that limit o space for redirect reached is Ok. |
#9111 allows manually excluding routes. Should the adapter warn the developer when a _redirects file is found and certain routes aren't excluded? Or should the adapter try to automatically include the routes found in _redirects? |
#9111 adds placeholders that allow automatic generation of exclude rules for things like _app, static files, and pre generated pages. I’m planning on adding another placeholder that would excludes redirects |
Here's a workaround in the meantime that reads the // svelte.config.js
const redirects = readFileSync('./static/_redirects', 'utf8')
.split('\n')
.filter(Boolean)
.map((line) => {
const [from] = line.split(' ');
if (!from) {
throw new Error(`Invalid _redirects entry: ${line}`);
}
return from;
}); const config = {
kit: {
adapter: adapter({
routes: {
include: ['/*'],
exclude: ['<all>', ...redirects]
}
}), |
Describe the bug
Cloudflare provides functionality to handle redirects using
![image](https://user-images.githubusercontent.com/1398808/220186952-725f56a2-db3d-454a-a5d4-ef2db0535624.png)
_redirects
config file (doc)When you try to use Cloudflare pages redirects with svelte kit project, then Cloudflare redirects are ignored.
It seems that __routes config, generated by sveltekit covers all routes with *
It means that all requests handled by worker script, before being passed to redirect.
Proposed solution:
add contents of _redirects to exclude section, like static assets.
Reproduction
Sorry, but I can't provide public Cloudflare account as reproduction.
I can only say that problem exists in any svelte kit project deployed to cloudflare pages.
Logs
No response
System Info
Severity
blocking an upgrade
Additional Information
No response
The text was updated successfully, but these errors were encountered: