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

Add config url trailing slash #4716

Closed

Conversation

tdugue
Copy link

@tdugue tdugue commented Aug 10, 2020

Related Issues

following the opening of this issue (#4684) I created a configuration ( urlTrailingSlash ) to add or not the slash at the end of a url.

I changed the normalizeUrlPath function so that depending on the configuration the slash is added or not.

I also changed the prepareDynamicRoute function and added the pathToRegexpOptions attribute with the strict option following the slash config. pathToRegexpOptions tells the vuejs router not to remove the slash.

Short Description and Why It's Useful

Screenshots of Visual Changes before/after (if There Are Any)

Which Environment This Relates To

Check your case. In case of any doubts please read about Release Cycle

  • Test version (https://test.storefrontcloud.io) - this is a new feature or improvement for Vue Storefront. I've created branch from develop branch and want to merge it back to develop
  • RC version (https://next.storefrontcloud.io) - this is a stabilisation fix for Release Candidate of Vue Storefront. I've created branch from release branch and want to merge it back to release
  • Stable version (https://demo.storefrontcloud.io) - this is an important fix for current stable version. I've created branch from hotfix or master branch and want to merge it back to hotfix

Upgrade Notes and Changelog

  • No upgrade steps required (100% backward compatibility and no breaking changes)
  • I've updated the Upgrade notes and Changelog on how to port existing Vue Storefront sites with this new feature

IMPORTANT NOTICE - Remember to update CHANGELOG.md with description of your change

Contribution and Currently Important Rules Acceptance

following the opening of this issue (vuestorefront#4684) I created a configuration ( urlTrailingSlash ) to add or not the slash at the end of a url.

I changed the normalizeUrlPath function so that depending on the configuration the slash is added or not.

I also changed the prepareDynamicRoute function and added the pathToRegexpOptions attribute with the strict option following the slash config. pathToRegexpOptions tells the vuejs router not to remove the slash.
@vercel
Copy link

vercel bot commented Aug 10, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/shopware-pwa/vue-storefront/9ffiioxho
✅ Preview: https://vue-storefront-git-fork-tdugue-fix-urltrailingslash.shopware-pwa.vercel.app

@vercel vercel bot temporarily deployed to preview August 10, 2020 09:27 Inactive
@CLAassistant
Copy link

CLAassistant commented Aug 10, 2020

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@tdugue
Copy link
Author

tdugue commented Aug 10, 2020

Hi,

After some test there are still problems the code does not work for all cases.

I try to update the PR quickly

Thanjs

Copy link
Contributor

@RakowskiPrzemyslaw RakowskiPrzemyslaw left a comment

Choose a reason for hiding this comment

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

please add changelog and change branch to hotfix/v1.12.3

@pkarw pkarw requested a review from gibkigonzo August 24, 2020 06:28
@gibkigonzo
Copy link
Collaborator

@tdugue can we do that without config? 🤔 I think it should work by default, wdyt?

@gibkigonzo gibkigonzo added the VSF1 Issues in regards to VSF1 label Sep 1, 2020
@gibkigonzo gibkigonzo changed the base branch from hotfix/v1.12.2 to hotfix/v1.12.3 October 2, 2020 13:05
@gibkigonzo
Copy link
Collaborator

@tdugue please rebase this branch to hotfix/v1.12.3 and update changelog in v1.12.3 section. Also I've added comments what should be changed to make it work. 🙏 (you may need to update test cases, but I'm not sure)

{},
userRoute,
routeData,
{ path: normalizedPath, name: `urldispatcher-${normalizedPath}`, pathToRegexpOptions: { strict: config.seo.urlTrailingSlash } }
Copy link
Collaborator

Choose a reason for hiding this comment

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

please remove this config and set strict to true. We want to use strict mode by default.

function prepareDynamicRoute (routeData: LocalizedRoute, path: string): RouteConfig {
  const userRoute = RouterManager.findByName(routeData.name)
  if (userRoute) {
    const normalizedPath = `${path.startsWith('/') ? '' : '/'}${path}`
    const dynamicRoute = Object.assign(
      {},
      userRoute,
      routeData,
      {
        path: normalizedPath,
        name: `urldispatcher-${normalizedPath}`,
        pathToRegexpOptions: { strict: true }
      }
    )
    return dynamicRoute
  } else {
    Logger.error('Route not found ' + routeData['name'], 'dispatcher')()
    return null
  }
}

@@ -57,7 +62,8 @@ export function findRouteByPath (path: string): RouteConfig {
export function normalizeUrlPath (url: string): string {
if (url && url.length > 0) {
if (url.length > 0 && !url.startsWith('/')) url = `/${url}`
if (url.endsWith('/')) url = url.slice(0, -1)
if (url.endsWith('/') && !config.seo.urlTrailingSlash) url = url.slice(0, -1)
if (!url.endsWith('/') && config.seo.urlTrailingSlash) url += '/'
Copy link
Collaborator

Choose a reason for hiding this comment

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

this needs to be parameterized, please change it to

export function normalizeUrlPath (url: string, clearTrailingSlash: boolean = true): string {
  if (url && url.length > 0) {
    if (url.length > 0 && !url.startsWith('/')) url = `/${url}`
    if (url.endsWith('/') && clearTrailingSlash) url = url.slice(0, -1)
    const queryPos = url.indexOf('?')
    if (queryPos > 0) url = url.slice(0, queryPos)
  }
  return url
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

this change probably require update in tests ☝️

@@ -57,7 +62,8 @@ export function findRouteByPath (path: string): RouteConfig {
export function normalizeUrlPath (url: string): string {
if (url && url.length > 0) {
if (url.length > 0 && !url.startsWith('/')) url = `/${url}`
if (url.endsWith('/')) url = url.slice(0, -1)
if (url.endsWith('/') && !config.seo.urlTrailingSlash) url = url.slice(0, -1)
if (!url.endsWith('/') && config.seo.urlTrailingSlash) url += '/'
const queryPos = url.indexOf('?')
if (queryPos > 0) url = url.slice(0, queryPos)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Last changes in different file:

  1. https://github.com/DivanteLtd/vue-storefront/blob/master/core/modules/url/router/beforeEach.ts#L26
    change const path = normalizeUrlPath(to.path) to const path = normalizeUrlPath(to.path, false) => in this place we removed trailing slash, so we need to use normalizeUrlPath with false which will result in keeping trailing slash
  2. https://github.com/DivanteLtd/vue-storefront/blob/master/core/modules/url/router/beforeEach.ts#L33
    change if (routeData) { to if (routeData && !routeData.name.endsWith('page-not-found')) {

@@ -92,6 +92,7 @@
"seo": {
"useUrlDispatcher": true,
"disableUrlRoutesPersistentCache": true,
"urlTrailingSlash": false,
Copy link
Collaborator

Choose a reason for hiding this comment

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

🔫 remove this 🙏

@Fifciu Fifciu mentioned this pull request Jan 5, 2021
6 tasks
@Fifciu
Copy link
Collaborator

Fifciu commented Jan 5, 2021

Due to the author's inactivity, I decided to finish this PR on my own: #5372

@Fifciu Fifciu closed this Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VSF1 Issues in regards to VSF1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants