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

fix: correctly restore trailing slash in url pathname for data requests #10475

Merged
merged 5 commits into from
Aug 11, 2023

Conversation

GingerAdonis
Copy link
Contributor

@GingerAdonis GingerAdonis commented Aug 3, 2023

Fixes #9595 and #10125.

Because DATA_SUFFIX is set to /__data.json an existing trailing slash gets omitted in a data request. This makes it impossible to know if the request URL pathname has a trailing slash. This issue normally applies only to routes with trailingSlash set to ignore in combination with a server load function.

This PR fixes this behaviour by adding a x-sveltekit-trailing-slash searchparam to data requests with a trailing slash so the server can restore the omitted trailing slash. This approach keeps the DATA_SUFFIX the same to ensure backwards compatibility. This was the least intrusive way I found to fix the issue.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented Aug 3, 2023

🦋 Changeset detected

Latest commit: af688ad

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I think the idea makes sense to signal the server that the trailing slash should be kept. I'm not 100% sure we need a query param for this, or if we could just do .../data.json/ instead.

Could you also add a test for this?

@GingerAdonis
Copy link
Contributor Author

GingerAdonis commented Aug 4, 2023

I'd say that adding a trailing slash to the data request itself might unnecessarily complicate the structure of the data requests URLs.

Another option would be to simply change DATA_SUFFIX from /__data.json to __data.json. Though I'm unsure if that affects compatibility in the various environments. What do you prefer?

I'll try to implement a test. :)

@GingerAdonis
Copy link
Contributor Author

I'm unsure how to approach a test for this. Could anyone point me in the right direction?

@eltigerchino
Copy link
Member

I'm unsure how to approach a test for this. Could anyone point me in the right direction?

Navigate to /packages/kit/test/apps/basics to spin up the test pages with pnpm dev.
The route /packages/kit/test/apps/basics/src/routes/routing/trailing-slash provides some sample pages to use when testing trailing slash conditions. You could add a load function to a new page under that directory to run tests on.

You can explore /packages/kit/test/apps/basics/test to find a existing tests to add the test next to (or maybe create a new suite?). I found reading the other tests as examples very helpful.

An example test could be fetching from a trailing slash endpoint and checking if it returns successfully using the expected URL structure.

@GingerAdonis
Copy link
Contributor Author

Thank you @s3812497 for the thorough explanation.

I've added tests for this PR. However, these tests will fail until #10531 is merged.

@GingerAdonis
Copy link
Contributor Author

I've included the changes from #10531 as requested. @dummdidumm

This should be good to go.

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dummdidumm dummdidumm merged commit 75d88d3 into sveltejs:master Aug 11, 2023
11 checks passed
@github-actions github-actions bot mentioned this pull request Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

trailingSlash ignore gives wrong URL pathname
3 participants