Skip to content

Preserve query string on redirect#609

Open
c-w wants to merge 2 commits into
evansd:mainfrom
c-w:preserve-redirect-on-query-string
Open

Preserve query string on redirect#609
c-w wants to merge 2 commits into
evansd:mainfrom
c-w:preserve-redirect-on-query-string

Conversation

@c-w

@c-w c-w commented Sep 19, 2024

Copy link
Copy Markdown

Hi there 👋

On one of my Django projects, I got tripped up by whitenoise stripping query strings on redirect, so for example /foo?bar=baz redirects to /foo/ when I was expecting it to instead redirect to /foo/?bar=baz. Would you be open to merging a patch that preserves the query string on redirect?

I'm opening a pull request to show what an implementation could look like and to start the discussion. If you're happy with the proposal, I can work on putting the functionality behind a setting (to not break backwards compatibility), unit tests, and what-ever else is needed.

Thanks for your time!

@Archmonger

Archmonger commented Sep 27, 2024

Copy link
Copy Markdown
Contributor

Given that WhiteNoise (and by extension ServeStatic) currently do nothing with query strings, what is the proposed use case of preserving query strings?

@c-w

c-w commented Sep 27, 2024

Copy link
Copy Markdown
Author

My use-case is that I'd like to have links to pages like /some/page?utm_source=value but whitenoise will currently redirect this to /some/page/ which means that client-side code loses access to the query param.

@Archmonger

Archmonger commented Sep 27, 2024

Copy link
Copy Markdown
Contributor

So if I understand correctly, in this scenario you (or some proxy) are creating/embedding tracking IDs into static files for tracking/metrics purposes?

@c-w

c-w commented Sep 28, 2024

Copy link
Copy Markdown
Author

Yes, essentially that's right. The example I used is for tracking conversion rates from Facebook ads. But the use case is actually still broader.

Concretely, I'm using whitenoise to serve a NextJS statically generated app. This means that I have several index.html files across various directories which get served, but within each of these files all logic happens client-side in React. So losing the query params on redirect means that I can't customise what happens on the page via shareable links like for example /some/page?search=foo to link to a search results page which will fetch the data for foo client-side.

Technically I can do what I need by including a trailing slash in all URLs (thus bypassing the redirect), but that's ugly and more importantly error prone as it's easy to forget.

@Archmonger

Archmonger commented Sep 28, 2024

Copy link
Copy Markdown
Contributor

Okay I understand the benefit. Would you be interested in duplicating this PR into ServeStatic?

For the last 5 years, things have been moving fairly slowly in WhiteNoise I don't know if/when this PR would get merged here.

@c-w

c-w commented Sep 30, 2024

Copy link
Copy Markdown
Author

@Archmonger Done, see Archmonger/ServeStatic#48

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.

2 participants