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

asset links with trailing slash true can be broken #6282

Open
6 of 7 tasks
dsmmcken opened this issue Jan 6, 2022 · 6 comments
Open
6 of 7 tasks

asset links with trailing slash true can be broken #6282

dsmmcken opened this issue Jan 6, 2022 · 6 comments
Labels
bug An error in the Docusaurus core causing instability or issues with its execution domain: trailing slash Related to trailing slash config status: accepting pr This issue has been accepted, and we are looking for community contributors to implement this

Comments

@dsmmcken
Copy link
Contributor

dsmmcken commented Jan 6, 2022

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

Description

I have a hosting provider where trailing slash true is required, and that works great. However, for direct links to files, it is broken.

I have a file in the docs folder (which is also versioned), and a link in .md doc like so

[Download this docx using Markdown](./assets/docusaurus-asset-example.docx)

However that link is transformed to .../assets/file/haash.docx/ and that link ends up as a 404, as it's treated as path by host, rather than resolving the file.

I think asset links should never get a trailing slash, but I don't have enough knowledge of other platforms to say that definitively.

Steps to reproduce

You can use the docusaurus docs to reproduce

  1. set trailing slash true
  2. npm run build
  3. npm run serve as an example server that is broken (I self host with Apache, and have the same problem)

On the /docs/markdown-features/assets#files page, attempt to open the .docx file links.

Expected behavior

It opens the linked file. File assets should probably never have trailing slashes, some hosts seem to be fine with it, but not all. I imagine all however a fine without for file assets.

Actual behavior

Document not found.

Your environment

  • Public source code:
  • Public site URL:
  • Docusaurus version used: beta.14
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): hosted with apache
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS):

Reproducible demo

No response

Self-service

  • I'd be willing to fix this bug myself.
@dsmmcken dsmmcken added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Jan 6, 2022
@Josh-Cena Josh-Cena added status: accepting pr This issue has been accepted, and we are looking for community contributors to implement this and removed status: needs triage This issue has not been triaged by maintainers labels Jan 7, 2022
@Josh-Cena
Copy link
Collaborator

This is actually not very easy to solve since we don't know what's a good heuristic for "assets" vs "pages"... I don't know if the static folder + pathname:// trick works for trailing slashes either, but worth trying out?

Note that our documentation has the same usage as well and a trailing slash is appended in deploy preview (we have trailingSlash: true for deploy preview and false for production). However, seems Netlify is smart enough to serve the correct file.

@slorber
Copy link
Collaborator

slorber commented Jan 12, 2022

Yes, the problem can be seen here BTW: https://deploy-preview-6320--docusaurus-2.netlify.app/docs/markdown-features/assets/#files

We can maybe add a new "hidden" prop in <Link> similar to the existing undocumented autoAddBaseUrl, something like autoApplyTrailingSlash, and then use it in the transformLinks MDX code? 🤷‍♂️ (or maybe a asset: true prop?)

We already have some heuristic there:

  const hasAssetLikeExtension =
    path.extname(assetPath) && !assetPath.match(/#|.md|.mdx|.html/);

But this probably doesn't make sense to apply this generically in the Link component

Using pathname:// prefix could work but it's not the same as the asset won't be converted to a require call and it would require to put it in /static

@Josh-Cena Josh-Cena added status: accepting pr This issue has been accepted, and we are looking for community contributors to implement this and removed status: accepting pr This issue has been accepted, and we are looking for community contributors to implement this labels Feb 7, 2022
@Josh-Cena Josh-Cena added the domain: trailing slash Related to trailing slash config label Mar 29, 2022
@stnor
Copy link
Contributor

stnor commented Aug 18, 2022

For people experiencing this issue, a workaround is to use absolute URL:s for the links.

@slorber
Copy link
Collaborator

slorber commented Aug 18, 2022

@stnor to avoid an absolute url, you should be able to put the file in /static/folder/file.xyz and ref it with [download file](pathname:///folder/file.xyz)

@stnor
Copy link
Contributor

stnor commented Aug 18, 2022

I am sorry if that was unclear.
The files folder is in /static/files the link is <a href="/files/foo.pdf" target="_blank">clickme</<a>

I get the 404 with the url /files/foo.pdf/ unless I use an absolute URL. On 2.0.1.

Are you suggesting it would work in markdown?

edit: oh is pathname:// some Docusaurus workaround? Does it work with <a href=""> too?

I'll give it a go.

@stnor
Copy link
Contributor

stnor commented Aug 18, 2022

Ok, got it. Nice that the pathname:// routing worked with html links too!
Found: https://docusaurus.io/docs/advanced/routing#escaping-from-spa-redirects

Thanks.

paulboocock pushed a commit to snowplow/documentation that referenced this issue Sep 7, 2022
paulboocock pushed a commit to snowplow/documentation that referenced this issue Sep 7, 2022
paulRbr added a commit to paulRbr/docs-3 that referenced this issue Feb 6, 2023
This changes the docusaurus config to avoid adding trailing slashes to
routes. This is necessary only to be able to serve static files with
no extension, due to a bug in docusaurus...

facebook/docusaurus#6282
nqst added a commit to transloadit/uppy.io that referenced this issue Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution domain: trailing slash Related to trailing slash config status: accepting pr This issue has been accepted, and we are looking for community contributors to implement this
Projects
None yet
Development

No branches or pull requests

4 participants