Skip to content

Express middleware for History API fallback - #303

Closed
sebdeckers wants to merge 1 commit into
webpack:masterfrom
sebdeckers:master
Closed

sebdeckers wants to merge 1 commit into
webpack:masterfrom
sebdeckers:master

Conversation

@sebdeckers

Copy link
Copy Markdown

The Connect middleware has issues with dots in URLs due to how it
redirects the request. The Express middleware is much simpler and
leverages Request.sendFile (Express 4.8.0 or later) to serve a fixed
file (index.html) only if the express.static middleware failed to find
a file. No rewriting needed, no issues with special characters in paths.

BTW there are no tests for this project so I would appreciate help in reviewing the change.

The Connect middleware has issues with dots in URLs due to how it
redirects the request. The Express middleware is much simpler and
leverages Request.sendFile (Express 4.8.0 or later) to serve a fixed
file (index.html) only if the express.static middleware failed to find
a file. No rewriting needed, no issues with special characters in paths.
@radotzki

Copy link
Copy Markdown
Contributor

I'm having the same problem with connect-history-api-fallback.
This pull request solved the problem for me.

@ChrisJamesC

ChrisJamesC commented Jun 15, 2016 •

Copy link
Copy Markdown

Any update on this PR? It solves a problem I also have. Can I do something to help speed up the adoption of a fix?
At test URL that failed before and now works is http://localhost:3000/hello.world

@ChrisJamesC

ChrisJamesC commented Jun 15, 2016 •

Copy link
Copy Markdown

An alternative fix is to replace the historyApiFallback=true line in the server config with:

historyApiFallback: {
    rewrites: [
        {
            from: /^\/app\/.*$/,
            to: function() {
                return 'index.html';
            }
        }
    ]
},

This will make any page with a dot in the /app path to be redirected to index.html (as it should be if it were not considered as a file)
See https://github.com/bripkens/connect-history-api-fallback#rewrites for more info

@radotzki

Copy link
Copy Markdown
Contributor

@ChrisJamesC thank you, that works for me!

@domenkozar

domenkozar commented Aug 6, 2016 •

Copy link
Copy Markdown

@sokra I'd consider this PR as the . limitation is really annoying. I mentioned this in bripkens/connect-history-api-fallback#25 (comment), but since this has been reported before it doesn't seem it will get fixed upstream.

@bripkens

bripkens commented Aug 7, 2016

Copy link
Copy Markdown

This is now fixed in connect-history-api-fallback and the . character rule can now be disabled.

@SpaceK33z

Copy link
Copy Markdown
Contributor

So, is this PR still relevant after the fix in connect-history-api-fallback?

@SpaceK33z

Copy link
Copy Markdown
Contributor

Closing this since it is now possible to do this as an option. This PR is a breaking change, and I don't think it's worth it.

@davidhariri

davidhariri commented Feb 28, 2017 •

Copy link
Copy Markdown

It would be helpful to mention what option we should specify @SpaceK33z

@tmcgann

tmcgann commented Jul 6, 2017 •

Copy link
Copy Markdown

Use the disableDotRule found here: https://github.com/bripkens/connect-history-api-fallback#disabledotrule. For example:

historyApiFallback: {
    disableDotRule: true,
}

I personally encountered issues with the rewrite rule--for some reason it changed the bundle that was used, which was really confusing. The symptom I observed was the hashes in my output files were different for the rewrite URLs; all the other URLs had the same hash in the output. Not sure why, may need to use the context argument as part of the rewrite (see https://github.com/bripkens/connect-history-api-fallback#rewrites). But the disableDotRule did the trick for me!

@roger-russel

Copy link
Copy Markdown

I tried with "disableDotRule: true" but disabled the other files as .js or .css, which made my vuejs project broke.

The solution that I found was based on this link.

was change this:
app.use(require('connect-history-api-fallback')());

To this:
app.use(require('connect-history-api-fallback')({
rewrites: [
{
from: /^/ip/allow/.*$/,
to: '/'
}
]
}))

The trick is create a rewrite to every route that uses dot on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants