Skip to content
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

fix(gatsby-plugin-nginx): remove trailing slash #1147

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/gatsby-plugin-nginx/src/nginx-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ function generateNginxConfiguration({
],
},

// Remove trailing slash if present
{
cmd: ['location', '~', '^(?<no_slash>.+)/$'],
children: [{ cmd: ['rewrite', '.+', '$no_slash'] }],
},

...prependLocations,
...locations,
...appendLocations,
Expand All @@ -201,17 +207,25 @@ function stringify(directives: NginxDirective[]): string {

const wildcard = /\*/g
const namedSegment = /:[^/]+/g
const catchAll = '/(.*)'

// Converts a gatsby path to nginx location path
// Ex:
// '/:p1/:p2/foo/*' => '^/([^/]+)/([^/]+)/foo/(.*)$'
// '/:splat' => '^/([^/]+)$'
// '/foo/bar/:splat' => '^/foo/bar/([^/]+)$'
export function convertToRegExp(path: string) {
const converted = path
let converted = path
.replace(wildcard, '(.*)') // replace * with (.*)
.replace(namedSegment, '([^/]+)') // replace :param like with url component like regex ([^/]+)

if (converted.endsWith(catchAll) && converted.length > catchAll.length) {
// allows '<path>/*' to serve '<path>' (without trailing slash)
// this is necessary because we are now removing trailing slashes from incoming requests
// so exact matches can work when there is a trailing slash
converted = converted.slice(0, -catchAll.length) + '(?:/(.*))?'
}

const noTrailingSlashes = normalizePath(converted)

return `^${noTrailingSlashes}$`
Expand Down
12 changes: 8 additions & 4 deletions packages/gatsby-plugin-nginx/test/nginx-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe('convert Gatsby paths into nginx RegExp', () => {
it('handles wildcard (*)', () => {
expect(convertToRegExp('/*')).toEqual('^/(.*)$')
expect(convertToRegExp('/*/')).toEqual('^/(.*)$')
expect(convertToRegExp('/pt/*')).toEqual('^/pt/(.*)$')
expect(convertToRegExp('/*/*')).toEqual('^/(.*)(?:/(.*))?$')
expect(convertToRegExp('/pt/*')).toEqual('^/pt(?:/(.*))?$')
})
})

Expand Down Expand Up @@ -109,7 +110,7 @@ describe('generateRewrites', () => {
},
{
children: [{ cmd: ['rewrite', '.+', '/pt/__client-side-search__'] }],
cmd: ['location', '~*', '"^/pt/(.*)$"'],
cmd: ['location', '~*', '"^/pt(?:/(.*))?$"'],
},
{
children: [{ cmd: ['rewrite', '.+', '/foo-path'] }],
Expand Down Expand Up @@ -138,7 +139,7 @@ describe('generateRedirects', () => {
it('correctly translates into NginxDirectives', () => {
const expected = [
{
cmd: ['location', '~*', '"^/api/(.*)$"'],
cmd: ['location', '~*', '"^/api(?:/(.*))?$"'],
children: [
{
cmd: [
Expand All @@ -150,7 +151,7 @@ describe('generateRedirects', () => {
],
},
{
cmd: ['location', '~*', '"^/graphql/(.*)$"'],
cmd: ['location', '~*', '"^/graphql(?:/(.*))?$"'],
children: [
{
cmd: [
Expand Down Expand Up @@ -279,6 +280,9 @@ describe('generateNginxConfiguration', () => {
deny all;
return 404;
}
location ~ ^(?<no_slash>.+)/$ {
rewrite .+ $no_slash;
}
location = /foo {
add_header a \\"b\\";
try_files /foo/index.html =404;
Expand Down