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

routes with and without trailing forward slash causes route "misses" #331

Closed
1 of 5 tasks
thescientist13 opened this issue Apr 18, 2020 · 9 comments · Fixed by #449
Closed
1 of 5 tasks

routes with and without trailing forward slash causes route "misses" #331

thescientist13 opened this issue Apr 18, 2020 · 9 comments · Fixed by #449
Assignees
Labels
Milestone

Comments

@thescientist13
Copy link
Member

thescientist13 commented Apr 18, 2020

Type of Change

  • New Feature Request
  • Documentation / Website
  • Improvement / Suggestion
  • Bug
  • Other (please clarify below)

Summary

As evidenced in PR #308, it looks like if there is or isn't a forward slash at the end of a route. e.g.

http://domain.com/about

vs.

http://domain.com/about/

Can lead to issues with matching against active routes in GraphQL / coming out of the Greenwood graph and even cause errors in the console, although I think Netlify will add / forward to the / so then it "catches".

77268328-ea8b5b00-6c7b-11ea-8cff-a53442cc83f5

Details

I think anywhere where we are matching against an active route or using window.location.hostname or similar to find out the current page, we will need to evaluate how it is being implemented and try and build a fairly robust / non fragile solution.

Also see #310 for reference.

@thescientist13 thescientist13 added bug Something isn't working P0 Critical issue that should get addressed ASAP labels Apr 18, 2020
@thescientist13 thescientist13 added this to the MVP milestone Apr 18, 2020
@thescientist13 thescientist13 added the v0.5.0 Data w/ GraphQL label Apr 22, 2020
@thescientist13 thescientist13 self-assigned this Apr 22, 2020
@thescientist13
Copy link
Member Author

thescientist13 commented Apr 22, 2020

Well, now adding the forward slash removes the undefined error, but adds this error now instead 😞 🤷
ex: https://deploy-preview-284--elastic-blackwell-3aef44.netlify.app/about/how-it-works/

Screen Shot 2020-04-22 at 6 28 41 PM

index.b37c464fdfd3e49a05d7.bundle.js:1 Uncaught (in promise) Invariant Violation: Invariant Violation: 8 (see https://github.com/apollographql/invariant-packages)
    at new e (https://deploy-preview-284--elastic-blackwell-3aef44.netlify.app/index.b37c464fdfd3e49a05d7.bundle.js:1:16081)
    at https://deploy-preview-284--elastic-blackwell-3aef44.netlify.app/index.b37c464fdfd3e49a05d7.bundle.js:117:58147
    at Array.forEach (<anonymous>)
    at n.diffQueryAgainstStore (https://deploy-preview-284--elastic-blackwell-3aef44.netlify.app/index.b37c464fdfd3e49a05d7.bundle.js:117:58105)
    at n.readQueryFromStore (https://deploy-preview-284--elastic-blackwell-3aef44.netlify.app/index.b37c464fdfd3e49a05d7.bundle.js:117:57465)
    at e.read (https://deploy-preview-284--elastic-blackwell-3aef44.netlify.app/index.b37c464fdfd3e49a05d7.bundle.js:117:67395)
    at e.n.readQuery (https://deploy-preview-284--elastic-blackwell-3aef44.netlify.app/index.b37c464fdfd3e49a05d7.bundle.js:117:47470)
    at https://deploy-preview-284--elastic-blackwell-3aef44.netlify.app/index.b37c464fdfd3e49a05d7.bundle.js:117:73775

Note: our site doesn't add the / so normal browsing should be fine, but in my case when forcing it, I noticed the above. One step forward, one step back. 👣

cc: @hutchgrant

@hutchgrant
Copy link
Member

hutchgrant commented Apr 24, 2020

For our site, the solution on netlify is:

Settings > Build & deploy > Post processing > Asset optimization then disable asset optimization and then disable pretty urls

Which eliminates forcing the trailing slash.

The problem is that we're caching routes without the trailing / and therefore when we visit URLs with the trailing / our cache does not contain a matching route which gives us these errors when it hydrates the graphql query.

Quick solution is to stop forcing trailing slash from netlify to begin with; its unnecessary.

We can probably close this issue because this is a special case and we cannot accommodate everyone's url rewrite rules. For netlify, maybe we can document this fix somewhere, maybe in build/deploy ?

Index pages will always have a trailing / and every other page will not have a trailing slash. This is assuming that the host doesn't rewrite the links (pretty urls via netlify). This is clear within the graph.js lifecycle:

// get md file's name without the file extension
let fileRoute = subDir.substring(seperatorIndex, subDir.length - 3);

// determine if this is an index file, if so set route to '/'
let route = fileRoute === '/index' ? '/' : fileRoute;

We do this so that we don't have links such as /docs/index and instead have /docs/. All pages within /docs/ won't have a trailing / e.g. /docs/markdown

@hutchgrant
Copy link
Member

hutchgrant commented Apr 24, 2020

We could also eliminate trailing slash from index page routes as well.

// determine if this is an index file, if so set route to '/'
let route = fileRoute === '/index' ? '' : fileRoute;

The issue with that is:

if you have a file /docs/index.md it would resolve to /docs while currently it resolves to /docs/.
if you also have a file /docs.md it would also resolve to /docs in both cases

We would have a duplicate route pointing to different pages.

@thescientist13
Copy link
Member Author

Good call @hutchgrant on Netlify settings .

Applied this setting for now, seems to have fixed the issue. 💯
Screen Shot 2020-04-24 at 1 58 36 PM

Let me add this issue to our Tuesday agenda to make sure there's nothing else we should be doing and / or documenting.

@thescientist13
Copy link
Member Author

So the question is: What should our default link format be, with or without a forward slash; /, and that either way it should be configurable, most likely from greenwood.config.js.

@thescientist13
Copy link
Member Author

thescientist13 commented May 1, 2020

From this PR, maybe anywhere we have to do some sort of matching, we should use a RegExp?

const route = window.location.pathname;

/* get some pages from GraphQL */

const currentPage = response[1].data.graph.filter((page) => {
  return new RegExp(`^${path}$`).test(list.item.link);
})[0];

Then we could only have /docs and it should still match if a route is /docs/? Then maybe no configuration? But maybe it would still be good to configure the / somehow? Not sure... 🤔

@thescientist13
Copy link
Member Author

thescientist13 commented May 6, 2020

Hmm, now i'm getting the cache invariants locally using the serve command. 😞
Screen Shot 2020-05-06 at 7 39 37 PM

Maybe an issue with the local web server prettifying URLs?

@thescientist13

This comment has been minimized.

@thescientist13 thescientist13 removed the P0 Critical issue that should get addressed ASAP label Nov 8, 2020
@thescientist13
Copy link
Member Author

I've normalized all routes as ending with / as part of #449

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 a pull request may close this issue.

2 participants