superstatic icon indicating copy to clipboard operation
superstatic copied to clipboard

Fix redirect as middleware

Open hugojosefson opened this issue 5 years ago • 1 comments

Regarding the following error:

  • You mount superstatic as a middleware on a path, instead of on no path (root), for example: app.use('/basepath', superstatic()) instead of app.use(superstatic()).
  • You make a request that superstatic is configured to redirect, such as /basepath/something/index.html/basepath/something.
  • The error was that superstatic did not take /basepath into account when redirecting, and would redirect only to the path below /basepath. In the example above, that would be to /something. When the client makes that new request to /something, express or connect responds 404 because superstatic is not mounted on that path.

This PR fixes that, by keeping req.url and req.originalUrl specifically apart for the purpose of redirects.

  • req.url containing information needed for mapping to a real file within the public directory, and
  • req.originalUrl containing information about how the request was actually made by the client. This is used for calculating correct redirects.

I updated files.spec.ts to run all its static file tests both with superstatic mounted as before, and as a middleware mounted under a base path.

hugojosefson avatar Oct 16 '20 12:10 hugojosefson

Rebased this fix on top of master. Apparently needs approval for PR build.

hugojosefson avatar Jun 07 '23 23:06 hugojosefson