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

Use settings.FORCE_SCRIPT_NAME correctly #486

Merged
merged 5 commits into from
Oct 28, 2024

Conversation

sarahboyce
Copy link
Contributor

@sarahboyce sarahboyce commented Apr 4, 2023

Hello 👋

This reverts this change #259 which added the use of get_script_prefix in the middleware init.
Unfortunately this function is not designed to be used outside the request response cycle and as middleware is initialised outside of that, this is causing unexpected results. For now reverting this change is the best solution to resolve this issue #271 👍

Note: You can see here a suggestion to have a better way to access this outside the request response cycle is being discussed

@sarahboyce sarahboyce marked this pull request as ready for review April 4, 2023 18:05
@sarahboyce sarahboyce force-pushed the script_prefix_setting branch 2 times, most recently from 484ceb8 to f6e5621 Compare April 6, 2023 06:19
@sarahboyce
Copy link
Contributor Author

@evansd @adamchainz I've now added a changelog, I think this is ready for a review 👍

@Archmonger
Copy link
Contributor

@sarahboyce Can you give some clarification on what "unexpected results" you are seeing with the current implementation?

@sarahboyce
Copy link
Contributor Author

@sarahboyce Can you give some clarification on what "unexpected results" you are seeing with the current implementation?

This should be mostly captured by this ticket to Django: https://code.djangoproject.com/ticket/34028
get_script_prefix is designed to be only used in the request response cycle. Here is is being used in the Middleware init (so during setup) and as a result settings such as FORCE_SCRIPT_NAME are not respected

@Archmonger
Copy link
Contributor

Archmonger commented Aug 30, 2024

Thanks for the clarification. Working on getting it merged within ServeStatic, a fork of WhiteNoise for continued maintenance and feature updates.

To leave some breadcrumbs just incase the WhiteNoise maintainers one day revisit the PR backlog:

  • The tests in this PR needed some love.
  • This PR needs some documentation updates to SERVESTATIC_STATIC_PREFIX
  • Everything else is otherwise looking good.

@adamchainz adamchainz changed the title Revert change to use get_script_prefix Use settings.FORCE_SCRIPT_NAME correctly Oct 28, 2024
@adamchainz adamchainz merged commit 9494ff3 into evansd:main Oct 28, 2024
13 checks passed
@adamchainz
Copy link
Collaborator

I got round to this eventually!

Thank you @sarahboyce for the references. Looks like there's still no better way to read the prefix. It would be good if Whitenoise could defer use of the prefix and read it lazily from the request, but that would be a much bigger refactor.

Thanks @Archmonger for the follow-up and details from ServeStatic. I added some and marked you as coauthor.

Releasing shortly.

github-actions bot added a commit to xshapira/fastapi-django-template that referenced this pull request Oct 29, 2024
Bumps [whitenoise](https://github.com/evansd/whitenoise) from 6.7.0 to
6.8.1.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/evansd/whitenoise/blob/main/docs/changelog.rst">whitenoise's
changelog</a>.</em></p>
<blockquote>
<h2>6.8.1 (2024-10-28)</h2>
<ul>
<li>
<p>Raise any errors from threads in the <code>whitenoise.compress</code>
command.</p>
<p>Regression in 6.8.0.
Thanks to Tom Grainger for the spotting this with a <code>comment on PR
[#484](evansd/whitenoise#484)
&lt;https://github.com/evansd/whitenoise/pull/484#discussion_r1818989096&gt;</code>__.</p>
</li>
</ul>
<h2>6.8.0 (2024-10-28)</h2>
<ul>
<li>
<p>Drop Django 3.2 to 4.1 support.</p>
</li>
<li>
<p>Drop Python 3.8 support.</p>
</li>
<li>
<p>Support Python 3.13.</p>
</li>
<li>
<p>Fix a bug introduced in version 6.0.0 where <code>Range</code>
requests could lead to database connection errors in other requests.</p>
<p>Thanks to Per Myren for the detailed investigation and fix in
<code>PR [#612](evansd/whitenoise#612)
&lt;https://github.com/evansd/whitenoise/pull/612&gt;</code>__.</p>
</li>
<li>
<p>Use Django’s |FORCE_SCRIPT_NAME|__ setting correctly.
This reverts a change from version 5.3.0 that added a call to Django’s
|get_script_prefix() method|__ outside of the request-response
cycle.</p>
<p>.. |FORCE_SCRIPT_NAME| replace:: <code>FORCE_SCRIPT_NAME</code>
__ <a
href="https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-FORCE_SCRIPT_NAME">https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-FORCE_SCRIPT_NAME</a></p>
<p>.. |get_script_prefix() method| replace::
<code>get_script_prefix()</code> method
__ <a
href="https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.get_script_prefix">https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.get_script_prefix</a></p>
<p>Thanks to Sarah Boyce in <code>PR
[#486](evansd/whitenoise#486)
&lt;https://github.com/evansd/whitenoise/pull/486&gt;</code>__.</p>
</li>
<li>
<p>Compress files using a thread pool.
This speeds up the compression step up to four times in benchmarks.</p>
<p>Thanks to Anthony Ricaud in <code>PR
[#484](evansd/whitenoise#484)
&lt;https://github.com/evansd/whitenoise/pull/484&gt;</code>__.</p>
</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/evansd/whitenoise/commit/bfc5dae69aad3abac5ff763421a4b4e2ae8ab378"><code>bfc5dae</code></a>
Version 6.8.1</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/6bbec0fe2be3814a38f3c7585414174053b414ea"><code>6bbec0f</code></a>
Raise errors from threads in whitenoise.compress (<a
href="https://redirect.github.com/evansd/whitenoise/issues/615">#615</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/0b054e5b9706c2c02b561e3f9ab78e8700c09b69"><code>0b054e5</code></a>
Version 6.8.0</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/54c464a48fec7e0a153f5243d71a25be5be98e12"><code>54c464a</code></a>
Upgrade and clarify Django quickstart docs (<a
href="https://redirect.github.com/evansd/whitenoise/issues/548">#548</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/d5caf8daed1a488999d92d33d3b91778003d1e6a"><code>d5caf8d</code></a>
Compress each file in a ThreadPool (<a
href="https://redirect.github.com/evansd/whitenoise/issues/484">#484</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/9494ff362264b03fb5882dcd059766eaea896ae5"><code>9494ff3</code></a>
Use settings.FORCE_SCRIPT_NAME correctly (<a
href="https://redirect.github.com/evansd/whitenoise/issues/486">#486</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/c42e93cd5805b0f057eefa72d99e4e10e0ae4f12"><code>c42e93c</code></a>
Make sure SlicedFile is closed properly (<a
href="https://redirect.github.com/evansd/whitenoise/issues/612">#612</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/f8dff5026750df7e793a2bc7bd3aaf18f3065a85"><code>f8dff50</code></a>
Drop Django 3.2 to 4.1 support (<a
href="https://redirect.github.com/evansd/whitenoise/issues/614">#614</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/6450820afc42a59e15924d5fc6efcbaa0ab626f6"><code>6450820</code></a>
Drop Python 3.8 support (<a
href="https://redirect.github.com/evansd/whitenoise/issues/613">#613</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/946a95eadb27edd475d64bda93c69cf70a887025"><code>946a95e</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/evansd/whitenoise/issues/611">#611</a>)</li>
<li>Additional commits viewable in <a
href="https://github.com/evansd/whitenoise/compare/6.7.0...6.8.1">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=whitenoise&package-manager=pip&previous-version=6.7.0&new-version=6.8.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>
mkjpryor pushed a commit to azimuth-cloud/azimuth that referenced this pull request Oct 29, 2024
Bumps [whitenoise](https://github.com/evansd/whitenoise) from 6.7.0 to
6.8.1.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/evansd/whitenoise/blob/main/docs/changelog.rst">whitenoise's
changelog</a>.</em></p>
<blockquote>
<h2>6.8.1 (2024-10-28)</h2>
<ul>
<li>
<p>Raise any errors from threads in the <code>whitenoise.compress</code>
command.</p>
<p>Regression in 6.8.0.
Thanks to Tom Grainger for the spotting this with a <code>comment on PR
[#484](evansd/whitenoise#484)
&lt;https://github.com/evansd/whitenoise/pull/484#discussion_r1818989096&gt;</code>__.</p>
</li>
</ul>
<h2>6.8.0 (2024-10-28)</h2>
<ul>
<li>
<p>Drop Django 3.2 to 4.1 support.</p>
</li>
<li>
<p>Drop Python 3.8 support.</p>
</li>
<li>
<p>Support Python 3.13.</p>
</li>
<li>
<p>Fix a bug introduced in version 6.0.0 where <code>Range</code>
requests could lead to database connection errors in other requests.</p>
<p>Thanks to Per Myren for the detailed investigation and fix in
<code>PR [#612](evansd/whitenoise#612)
&lt;https://github.com/evansd/whitenoise/pull/612&gt;</code>__.</p>
</li>
<li>
<p>Use Django’s |FORCE_SCRIPT_NAME|__ setting correctly.
This reverts a change from version 5.3.0 that added a call to Django’s
|get_script_prefix() method|__ outside of the request-response
cycle.</p>
<p>.. |FORCE_SCRIPT_NAME| replace:: <code>FORCE_SCRIPT_NAME</code>
__ <a
href="https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-FORCE_SCRIPT_NAME">https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-FORCE_SCRIPT_NAME</a></p>
<p>.. |get_script_prefix() method| replace::
<code>get_script_prefix()</code> method
__ <a
href="https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.get_script_prefix">https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.get_script_prefix</a></p>
<p>Thanks to Sarah Boyce in <code>PR
[#486](evansd/whitenoise#486)
&lt;https://github.com/evansd/whitenoise/pull/486&gt;</code>__.</p>
</li>
<li>
<p>Compress files using a thread pool.
This speeds up the compression step up to four times in benchmarks.</p>
<p>Thanks to Anthony Ricaud in <code>PR
[#484](evansd/whitenoise#484)
&lt;https://github.com/evansd/whitenoise/pull/484&gt;</code>__.</p>
</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/evansd/whitenoise/commit/bfc5dae69aad3abac5ff763421a4b4e2ae8ab378"><code>bfc5dae</code></a>
Version 6.8.1</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/6bbec0fe2be3814a38f3c7585414174053b414ea"><code>6bbec0f</code></a>
Raise errors from threads in whitenoise.compress (<a
href="https://redirect.github.com/evansd/whitenoise/issues/615">#615</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/0b054e5b9706c2c02b561e3f9ab78e8700c09b69"><code>0b054e5</code></a>
Version 6.8.0</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/54c464a48fec7e0a153f5243d71a25be5be98e12"><code>54c464a</code></a>
Upgrade and clarify Django quickstart docs (<a
href="https://redirect.github.com/evansd/whitenoise/issues/548">#548</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/d5caf8daed1a488999d92d33d3b91778003d1e6a"><code>d5caf8d</code></a>
Compress each file in a ThreadPool (<a
href="https://redirect.github.com/evansd/whitenoise/issues/484">#484</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/9494ff362264b03fb5882dcd059766eaea896ae5"><code>9494ff3</code></a>
Use settings.FORCE_SCRIPT_NAME correctly (<a
href="https://redirect.github.com/evansd/whitenoise/issues/486">#486</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/c42e93cd5805b0f057eefa72d99e4e10e0ae4f12"><code>c42e93c</code></a>
Make sure SlicedFile is closed properly (<a
href="https://redirect.github.com/evansd/whitenoise/issues/612">#612</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/f8dff5026750df7e793a2bc7bd3aaf18f3065a85"><code>f8dff50</code></a>
Drop Django 3.2 to 4.1 support (<a
href="https://redirect.github.com/evansd/whitenoise/issues/614">#614</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/6450820afc42a59e15924d5fc6efcbaa0ab626f6"><code>6450820</code></a>
Drop Python 3.8 support (<a
href="https://redirect.github.com/evansd/whitenoise/issues/613">#613</a>)</li>
<li><a
href="https://github.com/evansd/whitenoise/commit/946a95eadb27edd475d64bda93c69cf70a887025"><code>946a95e</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/evansd/whitenoise/issues/611">#611</a>)</li>
<li>Additional commits viewable in <a
href="https://github.com/evansd/whitenoise/compare/6.7.0...6.8.1">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=whitenoise&package-manager=pip&previous-version=6.7.0&new-version=6.8.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

3 participants