-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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 url navigation when window.___history does not exist #2667
Fix url navigation when window.___history does not exist #2667
Conversation
Deploy preview ready! Built with commit a4feba9 |
if (window.___history) { | ||
window.___history.push(pathname) | ||
} else { | ||
window.location.href = pathname |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @KyleAMathews , based on my investigation, gatsby-link
uses window.___navigateTo
to navigate between pages and window.navigateTo
relies on window.___history
to navigate. Apparently, when a resource/page not found in the directory, the host will use 404.html
to serve page not found and window.___history
does not seem to exist. Hence, using window.location.href
to navigate between pages.
Nice investigative work! This doesn't seem like the right fix though — is there any reason we can just ensure our history object is placed on ___window before 404 handling happens? |
Deploy preview failed. Built with commit 5fa690e https://app.netlify.com/sites/using-glamor/deploys/59f7ae20a6188f5421feeb84 |
I thought about it too before thought. Just that I have not dig into finding a way to ensure that the |
@KyleAMathews i just skimmed through just now and was wondering would it be fine to just attach/assign the history immediately right after line 24. Since |
Went to https://deploy-preview-2667--gatsbyjs.netlify.com/sdfsdf and still couldn't click to another page so looks like something is still broken. |
@KyleAMathews good finding. Was waiting for the notification of review app ready but you manage to got there early 😅 Alright, I'll investigate further later. Sorry about that. Thanks for confirming 🙇 |
…ge found during production and ensure a safe guard in component renderer checking
@@ -163,7 +163,8 @@ apiRunnerAsync(`onClientEntry`).then(() => { | |||
}) | |||
} else { | |||
return createElement(ComponentRenderer, { | |||
location: { page: true, pathname: `/404.html` }, | |||
page: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it into this way instead, since ComponentRenderer
is accepting and accessing page
props instead of getting it from location
props.
@@ -61,7 +61,10 @@ class ComponentRenderer extends React.Component { | |||
// This is only useful on delayed transitions as the page will get rendered | |||
// without the necessary page resources and then re-render once those come in. | |||
emitter.on(`onPostLoadPageResources`, e => { | |||
if (e.page.path === loader.getPage(this.state.location.pathname).path) { | |||
if ( | |||
loader.getPage(this.state.location.pathname) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could possibly be that the page resource might not exist. Putting a checking to guard from having to encounter error at this part.
Hi @KyleAMathews 😃 Based on this line of code for registering service worker, is there a way that we sort of being able to not always give a false alarm preventing the window to be reloaded? It seems to trigger multiple re-rendering in If we would like to preserve using Let me know what you have in mind as I could have overlook something 😺 Edit: Out of curiosity, I notice we have this line here to check for resources and run the DOM rendering through callback. Any reason for that being implemented that way compared to implementing it not through callback? Just like how it's done in root file 😅 |
…fallancy/gatsby into topic/fix-404-navigate-page
… serving 404 content is not feasible and would return unexpected result
… ComponentRenderer
Hey, what's the status of this? |
Deploy preview ready! Built with commit a4feba9 |
Hi @KyleAMathews , I am not particularly sure how to go forth from here. I would need your advice on this. Here's what I found:
Also referring to the old comment above, I notice that the DOM rendering has different approach compared to production and non production environment. I hope you'll be able to point out what I went wrong or what did I miss from here. |
@KyleAMathews I've tried to prevent multiple refresh when page not found. The glitch that appears on the page when page not found is caused by Let me know what you think or have in mind 😃 Edit: Apologies 🙇 I might get it wrong as I notice reloading also triggered by the emitter |
thanks for working on that 👍 😍 |
packages/gatsby/cache-dir/loader.js
Outdated
@@ -249,7 +254,8 @@ const queue = { | |||
|
|||
if (!page) { | |||
console.log(`A page wasn't found for "${path}"`) | |||
return | |||
cb() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this just be return cb()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews ah yes. You're right. I need to update this to be return cb()
Well looks like you're making progress :-) Can go to https://deploy-preview-2667--gatsbyjs.netlify.com/sdfsdf now and click a link. Not really sure what the blinking is from… looked a bit but don't have time for a deeper dive. I'd be fine merging this if you're running out of time to keep investigating as it's an improvement over what we have now or feel free to keep searching. |
@KyleAMathews I'm happy for you to merge it if you're happy with it 😄 I can do the search outside on different issue or PR. Also, if the inability to click on any link when page not found in production has impact on most app, I reckon we might need to merge. |
Thanks for all your work @emmafallancy! Let's put this out there! |
thanks ! 👏 |
Thanks @KyleAMathews 😄 |
@emmafallancy just a heads up I am on v1.9.114 and I am getting this error on a client-only route, |
Hi @ferdinandsalis , thanks for the heads up 😄 Would you be able to confirm if this is still happening in v1.9.117 ? |
Its still happening with v1.9.117. |
Do you have a sample repo that I can have a look at or poke around? I tried poking around with Gatsby's website and it seems that I can navigate around without hitting into the error that you mentioned. I can do a PR but I would actually like to understand how you managed to hit into that error 😅 @KyleAMathews have you encounter this issue before? What I can confirm is that if the 404 page uses layout, and when user navigate to other page from 404 page and going back again (by pressing back button), the layout will be gone and only renders the content of the 404 page. I can put a PR to this, along with the issue that you've found out. |
@emmafallancy thanks for taking a look at this. I will try to get repo up that reproduces the issue. Thanks for your help. |
This PR relates to #1838 . This would particularly fix the
history.push
for whenwindow.___history
does not exist, especially when no page found and the web host would serve404.html
page. Also,gatsby-link
depends onwindow.___navigateTo
and that also depends onwindow.___history
to navigate between pages.@KyleAMathews I hope this shall be able to fix the problem with navigating in
404
pages.