question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow configuration of `cleanUrls` option for `serve` command

See original GitHub issue

Have you read the Contributing Guidelines on issues?

Description

Allow configuration of the cleanUrls option passed to serve-handler package when running docusaurus serve.

Currently, Docusaurus sets the cleanUrls option for the serve-hander package to true and it isn’t configurable by the user. https://github.com/facebook/docusaurus/blob/bd9f29a4698963d3b139f7419877a300640b09ff/packages/docusaurus/src/commands/serve.ts#L76-L82

Proposing that this should be a user-configurable option.

Has this been requested on Canny?

No response

Motivation

By default, at least a few major hosting providers (GH Pages, Netlify, Vercel) don’t enable this ‘feature’. Having this as an option would allow for more accurate testing of the documentation website before deploying.

A Use Case: It is possible to embed an iframe into a documentation page which serves as a self-contained example. The example file can be loaded using the file-loader web pack loader.

<iframe
     src={require('!!file-loader!./assets/example.html').default}
 ></iframe>
 <a href={require('!!file-loader!./assets/example.html').default} target="\_blank">
     View example in a new window
 </a>

This works correctly when using docusaurus start and when deploying the build onto a hosting provider. However, when running docusaurus serve, any url ending with .html is rewritten without the extension and a 404 error occurs.

API design

ServeCLIOptions could include cleanUrls?: boolean; which defaults to true. https://github.com/facebook/docusaurus/blob/bd9f29a4698963d3b139f7419877a300640b09ff/packages/docusaurus/src/commands/serve.ts#L20-L25

When the user wishes to disable cleanUrls then they would call the serve command as follows: docusaurus serve --no-clean-urls

A similar options could be added to the configuration file (https://github.com/facebook/docusaurus/blob/bd9f29a4698963d3b139f7419877a300640b09ff/packages/docusaurus-types/src/config.d.ts).

Have you tried building it?

I’ve tried the changes locally and passing cleanUrls: false to serve-handler works as expected.

Self-service

  • I’d be willing to contribute this feature to Docusaurus myself.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SlicedSilvercommented, Aug 26, 2022

Within serve.ts, if I set cleanUrls: false then it works as expected.

However, if I remove the http 302 ‘found’ redirect logic then it works as well (without changing the cleanUrls):

    // if (!req.url?.startsWith(baseUrl)) {
    //   res.writeHead(302, {
    //    Location: baseUrl,
    //  });
    //  res.end();
    //  return;
    // }

Changing the Location value within the redirect also works (without changing the cleanUrls):

Location: baseUrl + req.url.slice(1),

If changing the location value is the best option then obviously it might make sense to check if baseUrl ends with / and url starts with / if that is quietly allowed within the configuration.

const slicePoint = (baseUrl.endsWith('/') && req.url.startsWith('/')) ? 1 : 0;
// ...
Location: baseUrl + req.url.slice(slicePoint),

@slorber Hope this helps, and thanks for your assistance.

1reaction
SlicedSilvercommented, Aug 24, 2022

I’ve created a stackbiltz example where the site functions correctly on npm run start, but not when running npm run serve.

https://stackblitz.com/edit/github-sifwoz?file=docusaurus.config.js

It appears that this behaviour only occurs when baseUrl is changed from the default of '/'.


The example:

  • Click ‘View Example’ on the landing page of the documentation site.
  • When run with start then you will see a light blue iframe
  • When run with serve then you will see the landing page within the iframe.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Enable clean URLs - Drupal
Before enabling clean URLs in the Drupal configuration screens (see below), you may need to prepare your server for clean URLs to work....
Read more >
How can I enable clean URLs? - Drupal Answers
The settings for the clean URLs are at admin/config/search/clean-urls. That page shows the "Enable clean URLs" checkbox only when it is ...
Read more >
Configure Hosting behavior | Firebase Hosting - Google
However, any connected custom domains will serve the configured value. Control .html extensions. Optional The cleanUrls attribute allows you to control whether ...
Read more >
linux - Why don't my clean URL's work in Drupal, even though ...
I use WSL on my physical device whenever I need a terminal for PS or Linux commands. I'm setting up a LAMP stack...
Read more >
Cisco Secure Firewall Threat Defense Command Reference - clf
This command leaves the clustering configuration intact so you can later re-add it to the cluster using the cluster enable command.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found