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

fix(utils): flat routes if child routes have absolute paths #7604

Merged
merged 1 commit into from Jun 25, 2020
Merged

Conversation

kedrzu
Copy link

@kedrzu kedrzu commented Jun 25, 2020

Fixed flatRoutes function to work properly if children routes have absolute paths.
This was working fine in 2.12, it did break in 2.13.

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

I am using nuxt.js along with nuxt-18n. I use SEO-friendly, localized routes.
For this to work, you have to configure pages to have absolute paths.

Here is a snapshot of my config:
image

However, it does not work well:
image

Routes are not properly concatenated. When I look into router.js generated file, it looks fine:
image

The reason behind this is, that flatRoutes function concatenates parent path and child path, even if child path is absolute.

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

// if child path is already absolute, do not make any concatenations
if (r.path && r.path.startsWith('/')) {
routes.push(r.path)
} else if (r.path === '' && fileName[fileName.length - 1] === '/') {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think plain old if-else is more readable than ternary operator with some parentness.

@codecov-commenter
Copy link

Codecov Report

Merging #7604 into dev will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##              dev    #7604   +/-   ##
=======================================
  Coverage   70.28%   70.28%           
=======================================
  Files          88       88           
  Lines        3745     3746    +1     
  Branches     1019     1018    -1     
=======================================
+ Hits         2632     2633    +1     
  Misses        904      904           
  Partials      209      209           
Flag Coverage Δ
#unittests 70.28% <100.00%> (+<0.01%) ⬆️
Impacted Files Coverage Δ
packages/utils/src/route.js 96.57% <100.00%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d89812f...7076b02. Read the comment docs.

@pi0
Copy link
Member

pi0 commented Jun 25, 2020

Hi @kedrzu. Thanks. This fix looks good to me btw why you are not using alias?

@pi0 pi0 changed the title fix(router): flat routes if child routes have absolute paths fix(utils): flat routes if child routes have absolute paths Jun 25, 2020
@kedrzu
Copy link
Author

kedrzu commented Jun 25, 2020

I don't think this would be an alternative.
What I use is a basic solution when using nuxt-i18n, described here: https://nuxt-community.github.io/nuxt-i18n/routing.html#custom-paths

I don't think aliasing would be properly recognized by nuxt-i18n to display page in proper language. I think in nuxt-i18n routes are recognized by their name:
image

Language is attached to the end of the route name. I don't want to mess with that :)

@kedrzu
Copy link
Author

kedrzu commented Jun 25, 2020

@pi0 maybe it would be better to cherry pick the commit to 2.x branch?

@pi0
Copy link
Member

pi0 commented Jun 25, 2020

Then it is a nuxt-i18n limitation to support aliases for CEO friendly version.

Fix looks good and dev branch is on fix mode so all good! Thanks for contribution ❤️

@pi0 pi0 merged commit 4a0cf8f into nuxt:dev Jun 25, 2020
@pi0 pi0 mentioned this pull request Jun 26, 2020
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants