Krullmizter
Krullmizter

Reputation: 567

Azure Blob Storage Static Website React SPA routing fails after Webpack compression changes

I'm working with a React Native Application web SPA, built with Webpack and hosted on Azure Blob Storage Static Website.

My issues began when I added the CompressionPlugin to speed up my build time and reduce my bundle size.

To optimize, I started configuring the CompressionPlugin to compress specific files, delete the original ones, and use a script to remove the .gz extension from the compressed files. Following are certain sections of my original Webpack configurations

Webpack module.exports output configuration:

output: {
  path: helpers.root("dist"),
  filename: "[name].[contenthash].js",
  chunkFilename: "chunk.[contenthash].js",
  clean: true,
},

Webpack CompressionPlugin configuration:

new CompressionPlugin({
    algorithm: "gzip",
    exclude: /env\//,
    test: /\.(js|json|xml|svg|txt|eot|ttf|css|html)$/,
    minRatio: Number.MAX_SAFE_INTEGER,
    deleteOriginalAssets: true,
}),

Webpack devServer configuration:

devServer: {
  static: { // Where static assets such as images are found
    directory: path.join(__dirname, "assets"),
  },
  host: "localhost",
  hot: true,
  port: 8090,
  compress: true, // gzip compression
  open: true,
},

Issue

When running the app locally with webpack serve --config config/webpack.development.js or http-server ./dist/ -g, everything works correctly, i.e., the client-side routing works as it should, and no errors.

However, after building and deploying to Azure, navigating directly to a route like /about results in a 404 Page Not Found error instead of the expected client-side routing taken over by the React Router.

Questions

What could be causing Azure Blob Storage Static Website to break client-side routing after applying compression and Webpack configuration changes?

Edit and answer my question:

After testing and looking through the documentation, I found the answers to my issues.

{
  "navigationFallback": {
    "rewrite": "/index.html"
  }
}
historyApiFallback: {
    disableDotRule: true, // Fixes issues with dots in paths
},

Updated Webpack configurations that made my setup work locally with Webpack's devServer and when building with Webpack and hosting it on Azure Static Website on Blob Storage.

Webpack base config:

module.exports = {
  target: "web",
  entry: "./src/index.js", // Root file. The config finds all JS and React files imports starting from the entry point
  output: {
    path: helpers.root("dist"),
    filename: "[name].[contenthash].js",
    chunkFilename: "chunk.[contenthash].js",
    publicPath: "/",
    clean: true,
  },

Webpack's devServer:

module.exports = webpackMerge(commonConfig, {
  mode: "development", // Sets development default values for several config options
  devtool: "eval-cheap-module-source-map",
  stats: "normal",
  devServer: {
    static: ["assets"], // Serve static files from the assets directory
    host: "localhost",
    hot: true, // Enables Hot Module Replacement (HMR)
    port: 8090,
    compress: true, // Gzip compression
    open: true,
    historyApiFallback: {
      disableDotRule: true, // Fixes issues with dots in paths
    },
  },

Webpack CompressionPlugin

plugins: [
  new CompressionPlugin({
    algorithm: "gzip",
    test: /\.(js|json|xml|svg|txt|eot|ttf|css|html|)$/,
    exclude: [/env\//, /index\.html/], // Since Azure static hosting doesn't allow 404 pages to be compressed, index.html needs to be uncompressed
    minRatio: Number.MAX_SAFE_INTEGER,
    deleteOriginalAssets: true,
  }),

Upvotes: -1

Views: 50

Answers (0)

Related Questions