-
Notifications
You must be signed in to change notification settings - Fork 505
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
"Uncaught SyntaxError: Unexpected token <" when refreshing page #165
Comments
Ok I think I have narrowed it down to the service-worker. Any ideas? |
If I For localhost, it says "from Service Worker" in the Size column (in Network tab). |
Does this happen in production builds? Pretty sure this is somehow related to |
@maxmilton Yes this is in prod builds. Cause what I think I'm experiencing is that index.html is cached, but after a new deploy, the bundles in the cached index.html are no longer there (due to changes in file name hash). However I might add that this is a bit of a modified setup. We're building a multi-tenant app, so we're creating an index.html for each tenant (and later in our build pipeline rename for example I'll just post our webpack.prod.config.js: 'use strict'
const fs = require('fs')
const path = require('path')
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const loadMinified = require('./load-minified')
const env = config.build.env
const webpackConfig = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({
sourceMap: config.build.productionSourceMap,
extract: true
})
},
devtool: config.build.productionSourceMap ? '#source-map' : false,
output: {
path: config.build.assetsRoot,
filename: utils.assetsPath('js/[name].[chunkhash].js'),
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
},
plugins: [
new CleanWebpackPlugin(['dist'], { root: path.join(__dirname, '..') }),
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
'process.env': env
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: true
}),
// extract css into its own file
new ExtractTextPlugin({
filename: utils.assetsPath('css/[name].[contenthash].css')
}),
// Compress extracted CSS. We are using this plugin so that possible
// duplicated CSS from different components can be deduped.
new OptimizeCSSPlugin({
cssProcessorOptions: {
safe: true
}
}),
// split vendor js into its own file
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function(module, count) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(path.join(__dirname, '../node_modules')) === 0
)
}
}),
// extract webpack runtime and module manifest to its own file in order to
// prevent vendor hash from being updated whenever app bundle is updated
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
chunks: ['vendor']
}),
// copy custom assets
new CopyWebpackPlugin([
{ from: path.resolve(__dirname, '../static/tracking'), to: '' },
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
},
{ from: path.resolve(__dirname, '../Dockerfile'), to: '' },
{ from: path.resolve(__dirname, '../.dockerignore'), to: '' },
{ from: path.resolve(__dirname, '../../docker/configure-fe-runtime.sh'), to: '' }
]),
// service worker caching
new SWPrecacheWebpackPlugin({
cacheId: 'booksecure-service-worker',
filename: 'service-worker.js',
staticFileGlobs: ['dist/**/*.{js,html,css}'],
minify: true,
stripPrefix: 'dist/'
})
]
})
for (let environment of config.build.environments) {
for (let tenant of config.build.tenants) {
let runtimeConfig = require(`../config/runtime/${environment}/runtime-config.${tenant}.js`)
webpackConfig.plugins.push(
// generate dist index.html with correct asset hash for caching.
// you can customize output by editing /index.html
// see https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: `${config.build.assetsRoot}/templates/${environment}/index.${tenant}.html`, // yes, no .html extension to not let these files be served.
template: 'index.html',
inject: true,
tenant: tenant,
runtimeConfig: `
<script id="runtimeConfig" type="text/javascript">
window.runtimeConfig = {
apiHost: '${runtimeConfig.API_HOSTNAME}',
apiKey: '${runtimeConfig.API_KEY}'
}
</script>`,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency',
serviceWorkerLoader: `<script>${loadMinified(
path.join(__dirname, './service-worker-prod.js')
)}</script>`
})
)
}
}
if (config.build.productionGzip) {
const CompressionWebpackPlugin = require('compression-webpack-plugin')
webpackConfig.plugins.push(
new CompressionWebpackPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + config.build.productionGzipExtensions.join('|') + ')$'),
threshold: 10240,
minRatio: 0.8
})
)
}
if (config.build.bundleAnalyzerReport) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}
module.exports = webpackConfig |
Okey guys! I've just verified that this is not actually a bug in the PWA template, but instead was related to the changes that we had made. Since we were not creating a var precacheConfig = [
['index.html', '91b2a317a09b223bf5a5582a5930069c'],
['static/css/app.3136db4aece9d70bd7458ade08e813c8.css', 'e685314d1221643e09338010621d36b5'],
['static/js/app.5a5b57a7c4b45cfcb1d8.js', '6045c0a02a2c055cefdb6d9d9e4574c7'],
['static/js/manifest.f52ceabe5f48b3ce1af2.js', 'fc67123c2142aea9e2dccf03ba3dbc5b'],
['static/js/vendor.85a0bf6fdf1309909410.js', 'e8f4ecbec293643c5f4b7b93744a4264']
], Once we added back the creation of index.html, the hash/id updated correctly. |
Glad to hear you found the culprit. It was a bit strange since I've not experienced it in any production builds before and I've worked on a lot of projects based on this template. Thanks for posting your solution too! |
i met the same problem ,i want to know how did you handle this ,counld u please explain it in detail? |
what did you do to let the hash/id update correctly? i don't understand exactly waiting your reply thank you very much |
app.use(express.static(path.join(__dirname, 'dist/'))); |
app.use(express.static(path.join(__dirname, 'dist/todos'))); after dist you should write the project name |
See this guys You just need to add Thanks |
@MikaelEdebro Can you please review and close this issue. |
server use static path, solve problem. |
I get the same issue but not on every refresh, only sometimes after I deploy the app. From what I understand, the first time I open the app it tries to load a JS files from the previous version (e.g. A refresh fixes this, except on progressive web apps on Safari because there is no reload button nor a "swipe down to refresh" action. It's very frustrating for users, as well as for me who asks them to restart their phones. Has anyone been in the same situation? |
@Dinduks did you find a solution to your problem? I'm having a similar issue where I get the error sometimes after deploys. |
I am having issues with this too. On every page refresh, the page gives this error. After a hard page refresh (ctrl + F5), it is fixed. Does someone know how to fix this? |
I am getting the same error when I deploy a new version and but a user already logged in my SPA loads a module dynamically in the previous version. I guess I would need to trap those errors and force a reload |
Hey @davidjnorth, I just noticed your message. I was thinking I'll just go the @pdemilly route and find some way to detect this problem then force a refresh. What did you guys do? |
Any idea to how to fix that? I am have a same issue I will force refresh to fix the issue but this is a forced fix. Any idea what is happening? |
Man this has saved my life. For my situation, I deploy my react client on express as the documentation of create-react-app said. Everything works fine, except when I refreshing a page whose URL contains a param. I've searched for a whole day. This is the only way which solved my problem perfectly for now. Hope it will help you guys, too. |
@Sonequa Thanks! Unfortunately that didn't work for me. I wonder if it's related to the fact that the error doesn't happen after every update for every users. |
same issue here.
|
@Dinduks You probably caches the index.html without the js files. So, when you build (deploy) app the js files changes (sometimes). But index.html is loaded from cache and has references to old js files. Error occurs sometimes because hash in the js files that you mentioned changes only when dependencies in package.json changes. |
A quick solution for this is to edit your |
Try adding |
I did the same here. It's quick and solves the issue. |
For us the issue was occurring because we had the build deployed on two instances. Sometimes the browser requested index.html from the Machine 1 and then asked the Machine 2 for styles and javascript which were different because of the difference in hashes when the build is built and minified. |
Facing the same issue. Where you able to find a solution or a workaround for this ? |
Are you deploying your build on multiple instances? |
I am posting this in a hope help others incase if they reach this far and still have not found a fix. So along with many other issues, one possibility is if you are using |
Hi guys! I had the same problem and i solved it inserting "no-cache" headers to server responce index.html. I think the problem exist because browser has his own cache in addition to cacheStorage. Cache-control: cache-control: no-store, must-revalidate I hope it will solve your problems |
Whenever I do a normal refresh, i get these error messages:
When looking in the Network tab, it´s like the entire HTML document (index.html) is being returned instead of the js file.
https://imgur.com/a/6AR5P
I'm assuming this has to do with the src to the js-files are returning 404. Question is why though?
If I hit hard refresh (Ctrl + F5), the site loads fine.
Does anyone have any clues?
The text was updated successfully, but these errors were encountered: