Skip to content

Express middleware for History API fallback #303

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

Closed
wants to merge 1 commit into from

Conversation

sebdeckers
Copy link

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
Contributor

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

@ChrisJamesC
Copy link

ChrisJamesC commented Jun 15, 2016

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
Copy link

ChrisJamesC commented Jun 15, 2016

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
Contributor

@ChrisJamesC thank you, that works for me!

@domenkozar
Copy link

domenkozar commented Aug 6, 2016

@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
Copy link

bripkens commented Aug 7, 2016

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

@SpaceK33z
Copy link
Member

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

@SpaceK33z
Copy link
Member

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
Copy link

davidhariri commented Feb 28, 2017

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

@tmcgann
Copy link

tmcgann commented Jul 6, 2017

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

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