-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
The Cloudflare adapter strips the trailing slash, causing the redirect to become a 404 #419
Comments
Having the same issue. |
I think a PR is welcome. However this needs to be fixed in the underscore redirects package inside the core repository, I guess. |
@alexanderniebuhr i found a way to clone the redirects on creation to add a trailing slash. diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts
index 3193683..ad9140f 100644
--- a/packages/cloudflare/src/index.ts
+++ b/packages/cloudflare/src/index.ts
@@ -67,6 +67,12 @@ export type Options = {
* for reference on how these file types are exported
*/
cloudflareModules?: boolean;
+
+ /**
+ * Duplicate redirect routes with added trailing slash. Defaults to true.
+ * When enabled, every entry in _redirects will be duplicated with an added trailing slash.
+ */
+ redirectAlsoWithTrailingSlash?: boolean;
};
function wrapWithSlashes(path: string): string {
@@ -342,6 +348,16 @@ export default function createIntegration(args?: Options): AstroIntegration {
});
if (!trueRedirects.empty()) {
+ if (args?.redirectAlsoWithTrailingSlash ?? true === true) {
+ const originalDefinitions = [...trueRedirects.definitions];
+ trueRedirects.definitions = [];
+ for (const definition of originalDefinitions) {
+ trueRedirects.definitions.push(definition);
+ trueRedirects.definitions.push({ ...definition, input: appendForwardSlash(definition.input) });
+ }
+ trueRedirects.minInputLength += 1;
+ }
+
try {
await appendFile(new URL('./_redirects', _config.outDir), trueRedirects.print());
} catch (error) { this does exactly what is needed: clone the urls with a trailing slash in correct order. maybe we could provide the option to the adapter to enable/disable this behaviour? but i'm not sure? |
Astro Info
Describe the Bug
Below, I have it set up to redirect from
/foo
or/foo/
to/bar
.However, when I build this with the Cloudflare adapter, the trailing slash is removed in the
_redirects
file, and accessing/foo/
results in a 404.What's the expected result?
The trailing slash has not been removed from the
_redirects
file, so it is expected that accessing/foo/
will result in a successful redirect.Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-fzcdgt?file=dist%2F_redirects
Participation
The text was updated successfully, but these errors were encountered: