Skip to content

fix: prepend basePath to static file URLs #1213

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

Merged
merged 7 commits into from
Feb 14, 2022
Merged

fix: prepend basePath to static file URLs #1213

merged 7 commits into from
Feb 14, 2022

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Feb 14, 2022

Summary

This partially fixes a complex bug that could manifest in several ways. There is an additional internal bugfix that needs to land in the proxy before this can be finally fixed.

Some of the details of the issue:

  • When Next attempts to read a pre-rendered page file from disk that has been moved to the CDN, the function makes a request to load that file, which it then caches. It constructs that URL by appending the requested path to the site domain. e.g. if Next tries to read /page.html and that file has been moved to the CDN, it will try to load https://example.com/page.html. It finds this domain using event.rawUrl.
  • Previously, this wasn't taking account of basePath, so was loading the URL without the basePath. This was fine when loading the site directly, because the actual files were in that place too.
  • There is a bug in proxy that means that the rawUrl passed to the proxied function is the original URL, rather than the proxied URL. There is no way for the function to know its own URL.
  • Because of this, when proxying from https://originsite/basePath to https://targetsite/basePath, it would try to load the files from originsite instead of targetsite, and because the path didn't match the proxy pattern, it was failing to load, giving a 404
  • Because of the way the Next server is memoized and files are cached, if the first request for a page was directly to the site rather than via the proxy, then it would correctly download and cache the file, and it would be available for subsequent requests via the proxy. This is why it was so hard to debug.

This fix

This fix ensures that the files moved to the CDN are moved under the correct basePath, meaning that the file docs.html is moved to basePath/docs.html. The proxy bug is still in existence, but because the basePath matches, the CDN request will also be proxied.

This PR also adds a test with with a basePath set. There aren't currently any cypress tests for it, but it can be manually tested. The proxy site is pointing at the deploy preview for this PR.

Test plan

  1. Visit the proxy site
  2. Click through to the Next site, then fallback and dynamic pages
  3. Ensure that they are not 404

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

Fixes #965

capybara and rabbits

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was published correctly.

@netlify
Copy link

netlify bot commented Feb 14, 2022

✔️ Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

🔨 Explore the source changes: 5c30f23

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/620a77f9295dab000812e4ed

😎 Browse the preview: https://deploy-preview-1213--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app/

@netlify
Copy link

netlify bot commented Feb 14, 2022

✔️ Deploy Preview for netlify-plugin-nextjs-export-demo ready!

🔨 Explore the source changes: 5c30f23

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/620a77fa55e6020008336b8f

😎 Browse the preview: https://deploy-preview-1213--netlify-plugin-nextjs-export-demo.netlify.app

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Feb 14, 2022
@netlify
Copy link

netlify bot commented Feb 14, 2022

✔️ Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

🔨 Explore the source changes: 5c30f23

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/620a77fa93c5d90008cfbf26

😎 Browse the preview: https://deploy-preview-1213--netlify-plugin-nextjs-static-root-demo.netlify.app

@netlify
Copy link

netlify bot commented Feb 14, 2022

✔️ Deploy Preview for netlify-plugin-nextjs-demo ready!

🔨 Explore the source changes: 5c30f23

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/620a77f996e11c000860fcf7

😎 Browse the preview: https://deploy-preview-1213--netlify-plugin-nextjs-demo.netlify.app

@cypress
Copy link

cypress bot commented Feb 14, 2022



Test summary

2 0 0 0


Run details

Project netlify-plugin-nextjs-nx-monorepo-demo
Status Passed
Commit 064fe3e ℹ️
Started Feb 14, 2022 3:30 PM
Ended Feb 14, 2022 3:31 PM
Duration 01:09 💡
OS Linux Ubuntu - 20.04
Browser Chrome 98

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@cypress
Copy link

cypress bot commented Feb 14, 2022



Test summary

21 1 0 0


Run details

Project netlify-plugin-nextjs-default-demo
Status Failed
Commit 064fe3e ℹ️
Started Feb 14, 2022 3:30 PM
Ended Feb 14, 2022 3:31 PM
Duration 01:35 💡
OS Linux Ubuntu - 20.04
Browser Chrome 98

View run in Cypress Dashboard ➡️


Failures

cypress/integration/default/middleware.spec.ts Failed
1 Middleware > should properly load custom headers

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@cypress
Copy link

cypress bot commented Feb 14, 2022



Test summary

7 0 0 0


Run details

Project netlify-plugin-nextjs-static-demo
Status Passed
Commit 064fe3e ℹ️
Started Feb 14, 2022 3:30 PM
Ended Feb 14, 2022 3:31 PM
Duration 01:15 💡
OS Linux Ubuntu - 20.04
Browser Chrome 98

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@ascorbic ascorbic changed the title fix: wip: prepend basePath to static file URLs fix: prepend basePath to static file URLs Feb 14, 2022
@ascorbic ascorbic marked this pull request as ready for review February 14, 2022 15:04
@ascorbic ascorbic self-assigned this Feb 14, 2022
@kodiakhq kodiakhq bot merged commit 8236b38 into main Feb 14, 2022
@kodiakhq kodiakhq bot deleted the mk/file-basepath branch February 14, 2022 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: 404s with dynamic routes and reverse-proxy using basePath
2 participants