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

urlParsed removes trailing slash #1629

Closed
openscript opened this issue May 2, 2024 · 9 comments
Closed

urlParsed removes trailing slash #1629

openscript opened this issue May 2, 2024 · 9 comments
Labels

Comments

@openscript
Copy link

Description

When the urlOriginal is /en/index.pageContext.json?asd=asd and in the address bar is displayed /en/?asd=asd the urlPathname is /en. When the urlOriginal is /en/?asd=asd the urlPathname is /en/.

This behavior is not influenced by disableUrlNormalization: true.

In my opinion the urlPathname should be /en/ when the urlOriginal is /en/index.pageContext.json?asd=asd.

@openscript
Copy link
Author

I would be happy to create a PR for this. Probably this lines need to be changed:

function removePageContextUrlSuffix(url: string): string {
const urlParsed = parseUrl(url, baseServer)
// We cannot use `urlParsed.pathname` because it would break the `urlParsed.pathnameOriginal` value of subsequent `parseUrl()` calls.
const { origin, pathnameOriginal, searchOriginal, hashOriginal } = urlParsed
assert(doNotCreateExtraDirectory === false)
const urlSuffix = `/index${pageContextJsonFileExtension}`
assert(pathnameOriginal.endsWith(urlSuffix), { url })
let pathnameModified = slice(pathnameOriginal, 0, -1 * urlSuffix.length)
if (pathnameModified === '') pathnameModified = '/'
assert(url === `${origin || ''}${pathnameOriginal}${searchOriginal || ''}${hashOriginal || ''}`, { url })
return `${origin || ''}${pathnameModified}${searchOriginal || ''}${hashOriginal || ''}`
}

Are there unit tests for this? Where would you add a unit test?

@brillout
Copy link
Member

brillout commented May 2, 2024

I agree and PR welcome 👍

I also agree that'd be good to add some Vitest test for it and create a file vike/vike/node/runtime/renderPage/removePageContextUrlSuffix.spec.ts. Let's export removePageContextUrlSuffix() and after the PR is merged I'll move removePageContextUrlSuffix() in its own file. (Let's move it to its own file later so it's easier for me to read the diffs you made to removePageContextUrlSuffix.)

@openscript
Copy link
Author

openscript commented May 2, 2024

Inside removePageContextUrlSuffix() we cannot know if it was originally /en/?asd=asd or /en?asd=asd. Should we read Vikes option trailingSlash and act according to that? If so, should trailingSlash be a param in removePageContextUrlSuffix() or for parseUrl()?

@brillout
Copy link
Member

brillout commented May 2, 2024

Yes, that's an issue: there is no way to know whether /en/index.pageContext.json is for a /en/ or /en URL. I'm thinking a trick could be to use /en//index.pageContext.json if the URL is /en/?

Inside removePageContextUrlSuffix() we cannot know if it was originally /en/?asd=asd or /en?asd=asd. Should we read Vikes option trailingSlash and act according to that? If so, should trailingSlash be a param in removePageContextUrlSuffix() or for parseUrl()?

That would be a brittle solution as it would fail for /en URLs then.

@brillout
Copy link
Member

brillout commented May 2, 2024

Ah, actually more natural: /en/index.pageContext.json/. Much clearer I think.

@openscript
Copy link
Author

openscript commented May 2, 2024

Where would the trailing slash /en/index.pageContext.json/ be added?

@brillout
Copy link
Member

brillout commented May 2, 2024 via email

@brillout
Copy link
Member

Closing as it doesn't seem fixable, see #1632 (comment).

@openscript Let me know whether this is a blocker for you. Otherwise I'll move on and focus on other priorities.

@brillout brillout closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@openscript
Copy link
Author

Thank you @brillout. Even though a nice way to handle this, I'm happy that we collaborated.

It's not a blocker as I can handle it case by case in my implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants