Skip to content
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

Cloudflare 404 for not excluded files in _routes.json with trailingSlash option #8708

Closed
korywka opened this issue Jan 24, 2023 · 8 comments · Fixed by #8733
Closed

Cloudflare 404 for not excluded files in _routes.json with trailingSlash option #8708

korywka opened this issue Jan 24, 2023 · 8 comments · Fixed by #8733
Labels
bug Something isn't working pkg:adapter-cloudflare
Milestone

Comments

@korywka
Copy link

korywka commented Jan 24, 2023

Describe the bug

If we have 100+ routes (actually ~4000) for prerender all routes that are NOT in excluded list:

"exclude":["/_app/*","/.DS_Store","/favicon.png","/global.css","/vite-manifest.json","/","/__data.json","/bin/vimdiff","/bin/vimdiff/__data.json"

like /bin/cat gives 404. But we have bin/cat/index.html file locally. So prerender works 100% correct. It is not missing file.

Removing export const trailingSlash = 'always'; seems like fixed that. It was quite easy after finding issue #8422 and need to check that prerendered pages (with/without trailing slashes) will still work with this setup. statement :)

Reproduction

This 4000+ routes are rendered from private SQLite, but I can share this project with some of your developers by email.
Video: #8708 (comment)

Logs

No response

System Info

System:
    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 87.28 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 19.4.0 - /opt/homebrew/bin/node
    npm: 9.2.0 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 109.0.5414.87
    Safari: 16.2
  npmPackages:
    @sveltejs/adapter-auto: 1.0.2 => 1.0.2 
    @sveltejs/kit: 1.2.6 => 1.2.6 
    svelte: 3.55.1 => 3.55.1 
    vite: 4.0.4 => 4.0.4

Severity

serious, but I can work around it

Additional Information

No response

@benmccann benmccann added p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc. pkg:adapter-cloudflare bug Something isn't working and removed p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc. labels Jan 24, 2023
@benmccann benmccann added this to the soon milestone Jan 24, 2023
@eltigerchino
Copy link
Member

eltigerchino commented Jan 25, 2023

The behaviour you're looking for can be achieved by changing prerender to 'auto':

// +page.js
export const prerender = 'auto'; // server will redirect '/bin/cat' to '/bin/cat/'
export const trailingSlash = 'always';

or you can include this in the file /static/_redirects to use Cloudflare Pages Redirects:

/bin/* /bin/:splat/

When using prerender = true, SvelteKit does not automatically redirect trailing slashes and vice-versa.
This is the same for all adapters.

With prerender = true and:

  • trailingSlash = 'always' , navigating to /bin/cat will result in a 404.
  • trailingSlash = 'never' (the default), navigating to /bin/cat/ will result in 404.
  • trailingSlash = 'ignore', navigating to /bin/cat/ will result in 404.

Perhaps this can be documented more thoroughly for people looking to retain the trailing slash redirection.

@korywka
Copy link
Author

korywka commented Jan 25, 2023

The most weird thing is that ~100 routes work correctly (that are excluded in cf routes config), and ~3900 are 404.

Imho, behavior should be consistent anyway.

@eltigerchino
Copy link
Member

eltigerchino commented Jan 25, 2023

The most weird thing is that ~100 routes work correctly (that are excluded in cf routes config), and ~3900 are 404.

I'd assume that Cloudflare is doing the redirection for the excluded routes.

The rest of the 3900 are being routed to the SvelteKit Cloudflare function, which doesn't have the routes in its manifest because of prerender = true. Changing this to prerender = 'auto' ensures they're included.

I set up this reproduction to test the behaviour of the trailingSlash option.
Running npm run build && npm run preview then navigating to /test/1 will result in a 404 despite /test/1/ being in the excluded list in /.svelte-kit/cloudflare/_routes.json

@korywka
Copy link
Author

korywka commented Jan 25, 2023

I will fake data and share my project. I need some time. Thank you for your interest.

@korywka
Copy link
Author

korywka commented Jan 25, 2023

@s3812497 please, take a look at this video:

with-slash.mov

I am preparing the project.

@korywka
Copy link
Author

korywka commented Jan 25, 2023

I will not make video without export const trailingSlash = 'always'; cause it works as expected.

@eltigerchino
Copy link
Member

@s3812497 please, take a look at this video:

with-slash.mov

Interestingly, I can access the prerendered page data https://5f68ec0e.binaries-web.pages.dev/bin/cat/__data.json but not the prerendered page itself https://5f68ec0e.binaries-web.pages.dev/bin/cat/index.html

Now I wonder if removing all the entries in exclude for _routes.json will cause the other prerendered routes to exhibit the same buggy behaviour.

@korywka
Copy link
Author

korywka commented Jan 26, 2023

You are right! __data.json is accessible, but not the .html! It is some kind of magic 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:adapter-cloudflare
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants