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

routesChunkName.json does not allow sub-route with same path as parent. #2917

Closed
slorber opened this issue Jun 10, 2020 · 0 comments · Fixed by #3001
Closed

routesChunkName.json does not allow sub-route with same path as parent. #2917

slorber opened this issue Jun 10, 2020 · 0 comments · Fixed by #3001
Labels
better engineering Not a bug or feature request bug An error in the Docusaurus core causing instability or issues with its execution difficulty: advanced Issues that are complex, e.g. large scoping for long-term maintainability. documentation The issue is related to the documentation of Docusaurus pr: maintenance This PR does not produce any behavior differences to end users when upgrading.

Comments

@slorber
Copy link
Collaborator

slorber commented Jun 10, 2020

TLDR: this is not urgent, and there is a workaround, just opened this issue for documenting the problem so that we don't forget to fix this later.

This is related to:
#2905 (comment)

Problem

Currently the routesChunkName.json is a mapping from path to chunkNames.

The problem is that you might have 2 routes with the same path. It notably happens in such case:

addRoute({
  path: "/",
  exact: false, // match all subpaths
  component: DocPage,
  routes: [
    { path: "/", exact: true, component: DocItem },
    { path: "/doc2", exact: true, component: DocItem },
    { path: "/doc3", exact: true, component: DocItem }
  ]
});

The DocsLayout component is supposed to intercept any request, apply the layout, and render the doc we are currently on.

But as we can make a doc the "home" (with homePageId), we end up with the parent and its child having the exact same path.

In such case, the routesChunkNames.json perform a bad "merge" of the data of the 2 routes having the same path:

  "/": {
    "component": "component---theme-doc-item-178-a40",
    "docsMetadata": "docsMetadata---docs-route-9-ee-json-ec1"
    "content": "content---ec-6-8fc"
  },

This lead to runtime errors, as D2 is not able to load all the required chunks for a given page, and the component attribute overridden by the children. (in such case / renders the doc directly, without using the layout comp)

We should ensure that this does not happen, and allow a parent and its child route to have the same path without troubles. The routesChunkName.json could for example evolve to a new format, taking an array of values instead of a single value.


Workaround:

The "parent" route can just omit the trailing /.

addRoute({
  path: "", // empty string!
  exact: false, // match all subpaths
  component: DocPage,
  routes: [
    { path: "/", exact: true, component: DocItem },
    { path: "/doc2", exact: true, component: DocItem },
    { path: "/doc3", exact: true, component: DocItem }
  ]
});

addRoute({
  path: "/next", // no trailing slash! as children has path = `/next/`
  exact: false, // match all subpaths
  component: DocPage,
  routes: [
    { path: "/next/", exact: true, component: DocItem },
    { path: "/next/doc2", exact: true, component: DocItem },
    { path: "/next/doc3", exact: true, component: DocItem }
  ]
});

The ReactRouter matching still works and routesChunkName.json is able to add 2 paths instead of merging them:

  "": {
    "component": "component---theme-doc-page-1-be-9be",
    "docsMetadata": "docsMetadata---docs-route-9-ee-json-ec1"
  },
  "/": {
    "component": "component---theme-doc-item-178-a40",
    "content": "content---ec-6-8fc"
  },

  "/next": {
    "component": "component---theme-doc-page-1-be-9be",
    "docsMetadata": "docsMetadata---nextb-2-e-c77"
  },
  "/next/": {
    "component": "component---theme-doc-item-178-a40",
    "content": "content---next-3-b-8-a1a"
  },
@slorber slorber 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 better engineering Not a bug or feature request difficulty: advanced Issues that are complex, e.g. large scoping for long-term maintainability. documentation The issue is related to the documentation of Docusaurus pr: maintenance This PR does not produce any behavior differences to end users when upgrading. v2 and removed status: needs triage This issue has not been triaged by maintainers labels Jun 10, 2020
slorber added a commit to slorber/docusaurus that referenced this issue Jun 10, 2020
slorber added a commit that referenced this issue Jun 17, 2020
* better fixes for docs homepage

* fix tests

* create special route for docs homepage + cleanup existing code

* no need to create multiple docs parent paths

* useful comment

* add test for slug + doc home usage at the same time error

* remove confusing variable name

* fix tests by using same suffix as before for docs base metadata path

* metadata: use homePageId correctly for nested docs: the full docId (including /) should be used to compare against homePageId

* add folder/testNested test doc

* refactor a bit processMetadata, the home should be handled correctly for all versions

* Workaround to fix issue when parent layout route (DocPage) has same path as the child route (DocItem): see #2917

* revert homePageId

* remove test doc

* remove test doc

* add useful comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
better engineering Not a bug or feature request bug An error in the Docusaurus core causing instability or issues with its execution difficulty: advanced Issues that are complex, e.g. large scoping for long-term maintainability. documentation The issue is related to the documentation of Docusaurus pr: maintenance This PR does not produce any behavior differences to end users when upgrading.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant