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

github pages 404 error on static files first time #4115

Closed
subsid opened this issue Feb 18, 2018 · 24 comments
Closed

github pages 404 error on static files first time #4115

subsid opened this issue Feb 18, 2018 · 24 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@subsid
Copy link

subsid commented Feb 18, 2018

I am new to gatsby and facing an issue with loading static html files.

I have a component called notes.js that links to some static files like so

export default ({ data }) =>
  <div style={{ flex: "1 0 auto" }}>
    <Link to={withPrefix("/notes/note1.html")} target="_blank">some note</Link>
  </div>

The file is saved at static/notes/note1.html.
This works fine in development, http://localhost:8000/notes/note1.html opens as expected when I click on the link.
But when I deploy the same, the first time I click on the link, it goes to github's 404 page.
But works fine on subsequent clicks.

Have I misconfigured something in gatsby?
gatsby repo: https://github.com/subsid/blog-skeleton

Thanks for your great work.

@peduarte
Copy link
Contributor

peduarte commented Feb 19, 2018

What error are you getting?

Is it like this?
image

Think I'm getting the same one.

@m-allanson
Copy link
Contributor

@peduarte I think your error looks different, could you open a separate issue?

@subsid Changing your <Link> components to plain <a> tags should fix it:

<a href={withPrefix("/notes/probability.html")} target="_blank">Probability</a>

I think the <Link> component is just for navigating within routes that Gatsby has created, so it doesn't know how to find your static html files.

Note that I get slightly different results using your site - in development mode clicking a link to one of the notes gives me a 404 error:

screen shot 2018-02-19 at 12 22 22

@peduarte
Copy link
Contributor

@m-allanson Hey 👋

Thanks for your suggestion, I'm going to give it a try.

I was under the impression Link should be used for linking between "Gatsby" Page Components.

In my case I am actually using NavLink (from gatsby-link) in order to apply a activeClassName prop.

Do you think this could be the issue?

@m-allanson
Copy link
Contributor

m-allanson commented Feb 19, 2018

Hi @peduarte 👋 that suggestion was for @subsid, I don't know if it'll help in your case. Could you create a new GitHub issue explaining the error you're seeing?

@peduarte
Copy link
Contributor

peduarte commented Feb 19, 2018

@m-allanson Sure!

@m-allanson
Copy link
Contributor

Thanks :)

@subsid
Copy link
Author

subsid commented Feb 19, 2018

@m-allanson thanks for looking into it. Changing it to an a tag doesn't seem to help.

The error seems like, my gatsby-config is not respecting the link to be a client only route. Actually the page opens, and then github's 404 shows up. (If you hit back on the 404 page, it goes to the notes file). Also, if I open the link manually in a new tab, it works fine. https://subsid.github.io/notes/probability.html Its only clicking on the link that causes the issue.

Regarding your local setup, hmm, not sure why that would happen. The file is present in static/notes/. Let me try to clone the repository again and see if I can reproduce it.
PS: The issue shows up only on github pages, not locally.

@subsid
Copy link
Author

subsid commented Feb 19, 2018

@m-allanson I was able to recreate the issue you had locally. The first time I cloned the repo, started server and clicked the link, I got the gatsby-404 page. But on subsequent clicks, it works fine.

But if I delete /public and start the server, it again goes to gatsby-404 the first time. Something to do with the file being cached? It's the same behavior on github pages as well. I suspect my gatsby-config more now. Should I handle static pages differently?

@m-allanson
Copy link
Contributor

Huh... ok! I'm not sure exactly what's happening under the hood, but I managed to work around this with these steps:

  • rename your /static/notes/ directory to something else - I chose /static/static-notes
  • update the links with the above new path
  • disable gatsby-plugin-offline

Does that fix it on your side? If you're able to do some more digging into the source of the problem that'd be very useful!

@subsid
Copy link
Author

subsid commented Feb 19, 2018

Yup, that solves the issue. Disabling gatsby-plugin-offline seems to cause someother error to do with sw.js when deployed. (might be cause of my chrome extensions as well) It worked fine on firefox though.
Anyways, I'll dig into this a little more and see if I can get more info. Thanks!

screen shot 2018-02-19 at 12 37 50 pm

@m-allanson
Copy link
Contributor

I'm glad you got it working! sw.js is likely the service worker installed by gatsby-plugin-offline to handle the offline side of things.

@wawoon
Copy link

wawoon commented Apr 18, 2018

I faced the almost same problem, and I fixed it by editing navigateFallbackWhitelist options of gatsby-plugin-offline.
In my case, service worker makes cache of the client-side only page.

It was very helpful for me to read docs of sw-precache because gatsby-plugin-offline uses sw-precache internally.

@pedrouid
Copy link
Contributor

I'm also facing the same issue, my question is how do you handle the already existing service worker to fix this issue or force refresh to update the changes.

@ykosaka-welyco when you updated navigateFallbackWhitelist did you manage to test on a different browser where the service worker hadn't been updated yet?

@imarem
Copy link

imarem commented May 18, 2018

@pedrouid I'm facing the same issue. Have you been able to find a solution?

@pedrouid
Copy link
Contributor

I have removed the service-worker completely and unregistered the existing ones

@kakadiadarpan kakadiadarpan added the type: question or discussion Issue discussing or asking a question about Gatsby label Sep 26, 2018
@SleeplessByte
Copy link

FWIW: not using gatsby-plugin-offline or any service worker and still having this issue.

It seems to occur because link clicks are hijacked by the router (which is fine) and subsequently var page = findPage(path) yields nothing. This is to be expected, because it's not a gatsby page but a static asset. This fails and now shows the 404 page.

When going there directly, there is no link click to be hijacked, and the gatsby router resolves correctly.

@gatsbot
Copy link

gatsbot bot commented Jan 19, 2019

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Jan 19, 2019
@SleeplessByte
Copy link

SleeplessByte commented Jan 19, 2019 via email

@gatsbot
Copy link

gatsbot bot commented Jan 30, 2019

This issue is being closed due to inactivity. Is this a mistake? Please re-open this issue or create a new issue.

@gatsbot gatsbot bot closed this as completed Jan 30, 2019
@SleeplessByte
Copy link

This is a mistake..

@wzard
Copy link
Contributor

wzard commented Feb 10, 2019

@SleeplessByte Did you figured out how to solve this issue? I am facing the same problem on production.

@SleeplessByte
Copy link

@subsid can you re-open this?
@wzard nope, we simply just leave it be.

@subsid
Copy link
Author

subsid commented Feb 10, 2019

@SleeplessByte I can't. I don't see the button reopen privileges

@wzard
Copy link
Contributor

wzard commented Feb 11, 2019

@SleeplessByte I added target="_blank" and href to http instead of https. It used to give 404 on primerlabs.io, but worked well on www.primerlabs.io. Now, it's working fine for both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

9 participants