Closed sedubois closed 5 years ago
It's related to https://www.gatsbyjs.org/docs/caching/#javascript
The gatsby-plugin-offline SW is caching JS so sometimes this means that a module will change internally but its name won't so things break... :-( Gatsby v2 (working on this now) which upgrades us to webpack 3 will solve this.
I'd kinda forgotten about this issue w/ the offline plugin. The solution there is we shouldn't cache JS files either. Would you like to put together a PR for that? We can re-enable JS caching in the SW for Gatsby v2.
@KyleAMathews Could it be solved if a parameter is on requests append like ?build1234567
on deployments?
I had the same error. I believe it is because the window.webpackManifest is written to the bottom of the body. So sometimes it is not initialized, when the components script are loaded and trying to access it. At least I haven't had the issues since I manually changed my html.js so that window.webpackManifest is added within the head tag.
my html.js looks like this
import React from "react"
let stylesStr
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
} catch (e) {
console.log(e)
}
}
module.exports = class HTML extends React.Component {
render() {
let css;
let mainfest;
if (process.env.NODE_ENV === `production`) {
// Add the chunk-manifest at the head section. As the async script loading of
// the components failes if the webpack-mainfest is missing.
const chunkManifest = require(`!raw!../public/chunk-manifest.json`)
mainfest = (<script
id="webpack-manifest"
key="webpack-manifest"
dangerouslySetInnerHTML={{
__html: `/*<![CDATA[*/window.webpackManifest=${chunkManifest}/*]]>*/`,
}}
/>
)
css = (
<style
id="gatsby-inlined-css"
dangerouslySetInnerHTML={{ __html: stylesStr }}
/>
)
}
return (
<html>
<head>
<meta charSet="utf-8" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
{mainfest}
{this.props.headComponents}
{css}
</head>
<body>
{this.props.preBodyComponents}
<div
key={`body`}
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
{this.props.postBodyComponents}
</body>
</html>
)
}
}
I'm aware this in an ugly hack, but I'm not familiar with the gatsby code base to actually make a PR.
@sjkp that's a separate issue which we solved recently by moving the scripts to below the manifest. They both used to be in the head then the manifest was moved to below the body https://github.com/gatsbyjs/gatsby/pull/3569
@dotmagic yes but that's worst as then scripts would need downloaded every time. Better to force 301 validations as that's far faster/cheaper.
Though harder to do with SW sigh
Let's just get Gatsby v2 out ASAP which will fix this for realz.
Hi @KyleAMathews ! I'm encountering the same issue and I didn't see the fact that JavaScript files shouldn't be cached for now. So my users are stuck with some erroneous files because I set expiry 30d
on these (now disabled).
Is there a quick way to specify a parameter like @dotmagic suggested before? Thank you!
@julien1619 not easy unfortunately — best bet is to post-process HTML files and add the query string programmatically to the script tags. You could use something like https://www.npmjs.com/package/cheerio in Gatsby's onPostBuild
extension point https://www.gatsbyjs.org/docs/node-apis/#onPostBuild
@KyleAMathews Thank you! Finally, I did a more manual change: I replaced all .js"
by .js?blabla"
in all build files.
Just ran into this issue also so I disabled SW caching for now :(
@zanedev what options did you use to disable caching within the plugin?
Sorry I mean I removed the offline plugin temporarily until it’s all fixed
That's what I ended up doing too. I'll revisit once v2 is official.
@lightstrike @zanedev did you need to do anything to unregister the already existing SW?
@pedrouid I did unfortunately and I didn't realize it until after I had released new features and everyone was freaking out. Not a fun position to be in :) This article helped me figure out how to clear them (near the bottom)
@zanedev Thanks! That worked perfectly
If caching doesnt work - you should remove it completely from your site and warn against using it - this is terrible. Just took me about 5 hours of staring at my css like wtf why arent these classes even on this div at all until I realized this.
I would point out to anyone here you probably need to manually remove your service worker or your users will just see empty content after you remove the offline plugin
if (typeof navigator !== 'undefined') {
navigator.serviceWorker.getRegistrations().then((registrations) => {
for (const registration of registrations) {
registration.unregister();
}
});
}
Can you guys try to use Webpack 4 not 3 ? Or better yet just wait until 5 and release up-to-date. If releasing v2 on an already old version of webpack that will be insanely behind since Webpack 5 is about to ship;
@bradennapier sorry you had trouble with this, Gatsby v2 is actively being worked on with the aim of getting v2 betas out as quickly as possible.
Until then, note that there's gatsby-plugin-remove-service-worker
that can handle unregistering for you. The Gatsby v2 alphas are currently running on webpack 4.
@m-allanson thanks!! Awesome good to hear you guys are updated to wp4 already. Appreciate the response. Great work on the overall solution - love Gatsby!
I am having the same issue, but getting this error: Refused to execute script from '<URL>' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
It happens about 15% of the time and the error goes away after reloading the page ~3 times.
Is it the same problem as above? Is there anyway to fix this right now?
This is my gatsby-config.js file:
module.exports = {
plugins: [
`gatsby-transformer-yaml`,
`gatsby-plugin-react-helmet`,
`gatsby-plugin-sass`,
{
resolve: `gatsby-source-filesystem`,
options: {
path: `./src/data/`,
},
},
{
resolve: 'gatsby-plugin-react-svg',
options: {
include: /assets/
}
},
{
resolve: `gatsby-plugin-netlify`,
options: {
headers: {
"/*.js": [
"Cache-Control: public, max-age=0, must-revalidate",
],
},
},
},
],
};
The problem only happens on the production build.
I opened an issue: #6947
I experienced similar happenings..I'm still on my way preparing to release my upgrade to v2 and don't know if this might also fix this? Anyone knows?
@CanRau see https://github.com/gatsbyjs/gatsby/issues/3582#issuecomment-358704657, normally webpack v3 fixes it, so yes Gatsby v2 should fix it. (I haven’t upgraded yet)
thanks for the hint..I guessed it might be fixed but good to read it ;)
@sedubois can you try upgrading gatsby and dependencies to their latest stable versions? We released version 2 recently, that might solve the issue for you.
@kakadiadarpan I propose to close this and will reopen if I encounter issues. (I had to move away from the codebase used in the OP; I will update it to v2 and push to production when possible.)
Hi Guys! When I run gatsby build
It gives me following error.
Building static HTML for pages{ Error: Cannot find module '!raw-loader!../public/styles.css'
gatsby develop
works fine. Could you guys please help me with this?
Hey @madhusudhan1234 sorry for the late response, were you able to figure it out?
Not sure about that !raw-loader!
think they're kind of out-dated and might interfere with some updates of webpack?
I have the same problem, production build fails. Worked fine just recently, maybe related to some package updates?
This is occuring on my site right as I used it to demo.
Not good man. Not good at all.
Description
Since I started using Gatsby a couple of months ago, I regularly get issues when visiting the published site. No issue in local development. The issue gets fixed when hitting Ctrl-Shift-R, but I can hardly expect all my visitors to do this every time...
I just see this error in the console:
The
bootstrap%2003bd3a01b7024eecaa93:52
file shows this:This is in a file whose description is
// install a JSONP callback for chunk loading
.Is this something to do with chunk loading which breaks because of gatsby-plugin-offline? My setup is very similar to the gatsbyjs.org codebase, so I think that the Gatsby site has the same issue, as I reported previously in #3142.
Do you have any idea of the underlying cause and the proper way to fix this? If there's no proper fix, is there a workaround such as disabling service worker entirely?
Unfortunately chunking and caching are still way above my skillset, but this is directly impacting our visitors.
Environment
Gatsby version: 1.9.158 and all my packages/plugins are up to date Node.js version: v8.9.4 (npm v5.6.0)
File contents (if changed):
gatsby-config.js
:package.json
:gatsby-node.js
:gatsby-browser.js
: not changedgatsby-ssr.js
: not changedActual result
The page only loads partially. Presumably, the browser only loads some cached assets but doesn't initialize Javascript properly. Visually the main image will remain blurred instead of loading fully. Or even the first redirect (from
/
to/en
) might also fail, so it doesn't load the content at all. This happens with my Firefox browser but also observed on my Chrome.Expected behavior
Page shouldn't fail to load.
Steps to reproduce
Happens regularly (but not always) when I deploy. Please let me know if there's something else I can provide to investigate.
Thanks a lot for your time and keep up the good work!