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

fix(core): docusaurus serve redirects should include the site /baseUrl/ prefix #10090

Merged
merged 2 commits into from
Apr 30, 2024

Conversation

slorber
Copy link
Collaborator

@slorber slorber commented Apr 30, 2024

Motivation

Fix #10078

docusaurus serve depends on vercel/serve-handler

This package is not super maintained and quite annoying to work with, particularly when we need to serve a static deployment under a baseUrl.

We can't do a rewrite from /baseUrl/:path to /:path, all requests end up as 404. I think the destination path must be an existing HTML files, not just a pathname.

Historically we have worked around that limitation thanks to the following hack:

    // Remove baseUrl before calling serveHandler, because /baseUrl/ should
    // serve /build/index.html, not /build/baseUrl/index.html (does not exist)
    req.url = req.url.replace(baseUrl, '/');

But the problem is that when the server handler is called with a trailingSlash: false setting for example, the responses will redirect /baseUrl/xyz/ to /xyz instead of /baseUrl/xyz. And the lib calls res.end() so there's no way to properly re-integrate that baseUrl to the redirect Location header.

So, this fix applies another workaround on top of serve-handler, so that we can avoid incorrect "trailing slash redirects" from serve-handler.

Test Plan

It's hard to test this automatically 😅 but I tested this locally under various scenarios

@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Apr 30, 2024
@slorber slorber requested a review from Josh-Cena as a code owner April 30, 2024 12:12
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Apr 30, 2024
Copy link

netlify bot commented Apr 30, 2024

[V2]

Name Link
🔨 Latest commit 70b4076
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/6630e01ce7b6f9000841d818
😎 Deploy Preview https://deploy-preview-10090--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟠 72 🟢 98 🟢 96 🟢 100 🟠 88 Report
/docs/installation 🟠 58 🟢 96 🟢 100 🟢 100 🟠 88 Report
/docs/category/getting-started 🟠 75 🟢 100 🟢 100 🟢 90 🟠 88 Report
/blog 🟠 70 🟢 100 🟢 100 🟢 90 🟠 88 Report
/blog/preparing-your-site-for-docusaurus-v3 🟠 61 🟢 96 🟢 100 🟢 100 🟠 88 Report
/blog/tags/release 🟠 71 🟢 100 🟢 100 🟠 80 🟠 88 Report
/blog/tags 🟠 75 🟢 100 🟢 100 🟢 90 🟠 88 Report

Copy link

netlify bot commented Apr 30, 2024

[V2]

Name Link
🔨 Latest commit 35321a8
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/6630e0422ab21100084178e8
😎 Deploy Preview https://deploy-preview-10090--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

Size Change: 0 B

Total Size: 1.59 MB

ℹ️ View Unchanged
Filename Size
website/.docusaurus/codeTranslations.json 2 B
website/.docusaurus/docusaurus.config.mjs 26.7 kB
website/.docusaurus/globalData.json 91.2 kB
website/.docusaurus/i18n.json 930 B
website/.docusaurus/registry.js 247 kB
website/.docusaurus/routes.js 156 kB
website/.docusaurus/routesChunkNames.json 109 kB
website/.docusaurus/site-metadata.json 2.17 kB
website/build/assets/css/styles.********.css 113 kB
website/build/assets/js/main.********.js 810 kB
website/build/index.html 38 kB

compressed-size-action

@slorber slorber merged commit 3ee7760 into main Apr 30, 2024
31 checks passed
@slorber slorber deleted the slorber/fix-server-handler-base-url-redirects branch April 30, 2024 12:28
@msyfls123
Copy link

@slorber

We can't do a rewrite from /baseUrl/:path to /:path, all requests end up as 404. I think the destination path must be an existing HTML files, not just a pathname.

I'm afraid that serve command failed to serve static files like 'index.js' with trailingSlash: true since it will always redirect to a 404 page.

@slorber
Copy link
Collaborator Author

slorber commented May 16, 2024

@msyfls123 if you think there's a bug please create a docusaurus.new repro with clear instructions on how to see the wrong behavior happening, and what is the expected behavior

@slorber
Copy link
Collaborator Author

slorber commented May 16, 2024

@msyfls123 probably this bug: #10139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problems with locale url on homepage and blog
3 participants