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

Duplicated whole page when accessing site by your-domain.com/index.html #9916

Closed
6 of 7 tasks
DmitryKorlas opened this issue Mar 6, 2024 · 4 comments · May be fixed by #10059
Closed
6 of 7 tasks

Duplicated whole page when accessing site by your-domain.com/index.html #9916

DmitryKorlas opened this issue Mar 6, 2024 · 4 comments · May be fixed by #10059
Labels
bug An error in the Docusaurus core causing instability or issues with its execution closed: duplicate This issue or pull request already exists in another issue or pull request

Comments

@DmitryKorlas
Copy link

DmitryKorlas commented Mar 6, 2024

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

Description

Accessing docusaurus build by direct URL domain.com/index.html causes a duplicated page issue.

It looks like shipped docusaurus serve CLI implements a redirect from index.html > /. And it is a specific logic which are not presented in http servers by default.

Background

I'm trying to migrate docusaurus 2.4.3>3.1.1 and faced an issues with merge-request integration in GitLab pipelines. GitLab pipeline produces a docusaurus build artifact which can be accessible by URL's like
some-domain.com/-/repoName/-/jobs/12345/artifacts/build/index.html.
Attempts to omit index.html part, i.e. some-domain.com/-/repoName/-/jobs/12345/artifacts/build/ caused of HTTP404 error. Probably it's possible to have a workaround fix by adjusting redirect rules for my case. Anyway, duplicated content behavior looks like a bug.

Reproducible demo

Steps to reproduce

  1. install from scratch
npx create-docusaurus@latest my-website classic --typescript
cd my-website/
npm install
npm run build
  1. run any http server to serve build over http (I'm using python http server to avoid side-effect redirects which shipped with docusaurus serve)
cd build
python3 -m http.server 9005 
  1. visit http://localhost:9005/index.html

Expected behavior

No duplicated content appears

Actual behavior

Whole site is rendered twice. Duplicated top-level menu and page content.

image
image

You can also observe this on https://docusaurus.io/index.html

image

Your environment

Self-service

  • I'd be willing to fix this bug myself.
@DmitryKorlas DmitryKorlas 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 Mar 6, 2024
@slorber
Copy link
Collaborator

slorber commented Mar 6, 2024

This has already been discussed here:

See my suggestions here: #9871 (comment)

You should serve pages by their canonical URLs. By default, this means serving them without the .html extension.

If this is a problem, there's often a "pretty URL" option on CDNs and static hosts you can turn on, otherwise you can use reverse proxy settings.

@slorber slorber closed this as not planned Won't fix, can't repro, duplicate, stale Mar 6, 2024
@slorber slorber added closed: duplicate This issue or pull request already exists in another issue or pull request and removed status: needs triage This issue has not been triaged by maintainers labels Mar 6, 2024
@DmitryKorlas
Copy link
Author

Thanks for your response and valuable feedback! Maybe it would be helpful to mention about new behavior at the migration guides. I followed the instructions step by step, but this behavior was unexpected.

@DmitryKorlas
Copy link
Author

Regarding my specific case with GitLab artifacts deployment for merge requests, I couldn't find the ability to implement a redirect on the GitLab side. GitLab integration requires an index.html file for viewing artifacts of MR.

I added a workaround trick to replace the URL on the client side. It looks messy, and I don't like it, but it resolved the issue. Perhaps it can be helpful to someone else:

// docusaurus.config.ts
import type {Config, HtmlTagObject} from '@docusaurus/types';

// see https://github.com/facebook/docusaurus/issues/9916
const redirect = `
(function() {
  var pathname = window.location.pathname;
  if (pathname.endsWith('index.html')) {
    history.replaceState(
      null,
      pathname,
      pathname.replace('index.html', '')
    );
  }
})();
`.replace(/\n/g, '');

const config: Config = {
  // ...
  headTags: [
    {
      tagName: 'script',
      attributes: {
        src: 'data:text/javascript,' + redirect,
      },
    },
  ] satisfies HtmlTagObject[],
};

@slorber
Copy link
Collaborator

slorber commented Apr 19, 2024

We'll try to fix this for V4 in #10059

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 closed: duplicate This issue or pull request already exists in another issue or pull request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants