question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[webpack-index-html-plugin] output script src prevents single-page dev server

See original GitHub issue

Description

It is currently impossible to deploy and debug single-page applications because the entrypoint script’s URL is relative.

Current behaviour

In the index.html it might be <script type="module" src="./components/my-app.js"></script>.

Both in production build as well as with webpack-dev-server this will result in a relative script like <script src="./my-app.44645eba9e3994eab753.js">

This breaks single-page apps because when requesting any non-root URL like /some/virtual/path the script (as well as dynamically loaded chunks) will be fetched from /some/virtual/my-app.44645eba9e3994eab753.js which responses with a 404 status.

Expected behaviour

The generated script should be absolute. In the example above is should only mean to remove the dot => /my-app.44645eba9e3994eab753.js.

Workaround for dev server

I managed to tweak the dev server and output config to get the local server running smoothly by explicitly rewriting the entry point scripts and setting the publicPath (necessary to get correct URL to chunks):

devServer: {
  publicPath: '/',
  historyApiFallback: {
    rewrites: [{ from: /my-app.js$/, to: '/my-app.js' }],
  },
},
output: {
  publicPath: '/',
},

Apparently the only change necessary for the deployed build is to change the script paths in index.html. The chunks are otherwise already loaded correctly.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
tpluscodecommented, Sep 18, 2019

What if the plugin injected a base tag as <base href="${config.output.publicPath}"> if the tag is not otherwise present in the source index?

0reactions
stale[bot]commented, Nov 9, 2019

Hey there, it’s me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Open Web Components!

Read more comments on GitHub >

github_iconTop Results From Across the Web

HtmlWebpackPlugin | webpack
The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. ... entry: 'index.js', output: { path: path.resolve(__dirname, '.
Read more >
browser automatic reload when index.html changes
I finally stumbled across html-webpack-plugin as described in this guide. I run: npm i -D html-webpack-plugin. And edited my webpack.config.js to look like ......
Read more >
Can this works with webpack-dev-server #3 - GitHub
During dev I'm running webpack-dev-server, so my index.html has a script tag pointing at http://localhost:1234/my-entry-bundle.js
Read more >
Webpack: Part-2 - Medium
const HtmlWebpackPlugin = require('html-webpack-plugin');module.exports = { mode : "development" entry: "./src/index.js", output:
Read more >
Using webpack with TypeScript - LogRocket Blog
With webpack, TypeScript code is compiled into a JavaScript file that ... for your project is src/index.js and will output the minified and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found