capouch
capouch

Reputation: 577

Dot and hyphen disallowed React Router URL parameters?

I am working to implement an image viewer and using React Router. Uploaded image files are of the format <name>.<type-suffix>-<date-tag>, with a period and a hypen as delimiters.

Given this route: <Route path="zoomer/:imageId" component={ Zoom }/> and this URL http://localhost:8080/zoomer/medMain.tif-1461839237863 it does not seem that the router is finding a match.

If I remove the dot and the hyphen (e.g. http://localhost:8080/zoomer/medMaintif1461839237863) routing works just fine, but I really need to keep those delimiters for semantic reasons. And URLEncode() won't help me here, either.

Is there something I need to do with the Route spec to fix this?

Upvotes: 14

Views: 6955

Answers (3)

Salvatorelab
Salvatorelab

Reputation: 11873

If you are using Vite then this plugin will solve the issue:

Install it as a dev dependency

npm install --save-dev vite-plugin-rewrite-all

Add the plugin to your vite.conf.js file

import pluginRewriteAll from 'vite-plugin-rewrite-all'; // <= import the plugin


export default defineConfig({
  plugins: [
    react(),
    pluginRewriteAll() // <= add the new plugin
  ],
})

Source: https://github.com/ivesia/vite-plugin-rewrite-all

Upvotes: 4

pors
pors

Reputation: 4054

Adding this to your webpack devServer config also does the trick:

historyApiFallback: {
    disableDotRule: true
}

Upvotes: 19

kalmanb
kalmanb

Reputation: 36

I had the same issue proved to be webpack dev server with history-api-fallback enabled failed to pass these urls to the react app. Hacked webpack config to pass these to react with:

...
devServer: {
  proxy: {
    '/*.*': { // Match all URL's with period/dot
      target: 'http://localhost:8080/',  // send to webpack dev server
      rewrite: function(req){
        req.url='index.html';  // Send to react app
      }
    }
  }
}
...

Upvotes: 2

Related Questions