whitenoise icon indicating copy to clipboard operation
whitenoise copied to clipboard

Preserve query string on redirect

Open c-w opened this issue 1 year ago • 6 comments

Hi there :wave:

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!

c-w avatar Sep 19 '24 15:09 c-w

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

Archmonger avatar Sep 27 '24 07:09 Archmonger

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.

c-w avatar Sep 27 '24 09:09 c-w

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

Archmonger avatar Sep 27 '24 09:09 Archmonger

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.

c-w avatar Sep 28 '24 11:09 c-w

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.

Archmonger avatar Sep 28 '24 20:09 Archmonger

@Archmonger Done, see https://github.com/Archmonger/ServeStatic/pull/48

c-w avatar Sep 30 '24 10:09 c-w