Skip to content

Conversation

@florian-sattler
Copy link
Contributor

Added nginx deployment documentation as discussed in #3756 with some links for further reading.

@brc-dd brc-dd merged commit f65053c into vuejs:main Apr 10, 2024
@brc-dd
Copy link
Member

brc-dd commented Apr 28, 2024

This doesn't handle the case of removing trailing slash. /foo/bar/ should also check for /foo/bar.html when cleanUrls is enabled.

@florian-sattler
Copy link
Contributor Author

@brc-dd Yes indeed. Removing the trailing slash by redirecting also has downsides: https://blog.mehdi.cc/articles/vitepress-cleanurls-on-nginx-environment#the-trade-off-for-index-pages

Using the currently described setup:

  • When accessing a folder which has an index.html file this will get delivered whether or not using a trailing "/"
  • When trying to access a document (e.g. example.html) this will break if being accessed using a trailing "/" (the page gets shown however filled from the error page which results in a broken sidebar as discussed in Broken sidebar when loading page #3756)

I'm currently on vacation, so I couldn't test the behavior when there is a folder (example/) along a file with the same name (example.html). This might indeed break.

Did I miss a (easy) way to handle this?

@brc-dd
Copy link
Member

brc-dd commented Apr 28, 2024

I don't think that blog post is doing things correctly. They are doing 301 redirects while vitepress explicitly states you need rewrites for cleanUrls to work (rewrites here are different than nginx rewrite directive). Rewrite basically means serving the page without doing 301/302. I'll try and update the configuration to handle that case. Basically all these cases need to be handled - https://github.com/slorber/trailing-slash-guide (the "both" test case there is ambiguous and left out to implementation. One needs to do what Netlify is doing for cleanUrls to work properly)

@meduzen
Copy link

meduzen commented Apr 28, 2024

Hi! Author of the article here.

What I do

I don't think that blog post is doing things correctly. They are doing 301 redirects while vitepress explicitly states you need rewrites for cleanUrls to work (rewrites here are different than nginx rewrite directive)

Actually I do both. My goal is that the user is always on the canonical (no trailing / nor .html), so:

  • I redirect to make sure the clean URL is actually clean.
  • Once redirected, I do rewrite to make sure the right path/to/file.html gets served.

For example, to serve the file at https://blog.mehdi.cc/articles.html:

(And I also want all URLs to work without JavaScript in the browser. But in a way or another, static site generators have their limits, and looking at what hosting service do, there are redirects and URL inconsistencies (user point of view) in most clean URLs scenarios. So choices have to be made and URL consistency is very important for me.)

Alternatively

Since I provide a <meta name="canonical"> I could just skip the redirects, but it seems not easy with Nginx to cover all use cases, like for example the one you describe:

This doesn't handle the case of removing trailing slash. /foo/bar/ should also check for /foo/bar.html when cleanUrls is enabled.

I can’t have this without first getting rid of the trailing slash with a redirect. And then it creates more issues if. More on this in next section.

Testing domain and branch

To help with testing purpose, I opened a branch and a new subdomain:

  • the only change on the branch is to moves /notes.md to /notes/index.md (while /articles.md remains at the same place).
  • the subdomain https://test.blog.mehdi.cc applies the nginx config described in Vitepress doc (so the one from this PR, not the one from my article).

When doing this:

I also tested with rewrite ^(.+)/+$ $1; (remove trailing slash) to enable the use case quoted in the previous section (/foo/bar/): it works fine in all scenarios, but:

(This is not the config live on the test. subdomain but I can enable it if you want to have a look.)

Let me know if you would like to test specific things.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants