Skip to content

Commit

Permalink
fix(gatsby-plugin-nginx): Spread routes support (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored Aug 16, 2021
1 parent 1af5d4e commit 6bb2331
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-nginx/src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function cacheHeadersByPath(pages: Page[], manifest: Manifest): PathHeadersMap {
}

// removes trailing slash if possible
function normalizePath(path: string) {
export function normalizePath(path: string) {
if (!path.endsWith('/') || path === '/') {
return path
}
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-plugin-nginx/src/nginx-generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { posix } from 'path'

import { INDEX_HTML, LOCATION_MODIFIERS } from './constants'
import { normalizePath } from './headers'

export {
stringify,
Expand Down Expand Up @@ -205,7 +206,9 @@ export function convertToRegExp(path: string) {
.replace(wildcard, '(.*)') // replace * with (.*)
.replace(namedSegment, '([^/]+)') // replace :param like with url component like regex ([^/]+)

return `^${converted}$`
const noTrailingSlashes = normalizePath(converted)

return `^${noTrailingSlashes}$`
}

function isRegExpMatch(path: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-nginx/test/nginx-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('convert Gatsby paths into nginx RegExp', () => {

it('handles wildcard (*)', () => {
expect(convertToRegExp('/*')).toEqual('^/(.*)$')
expect(convertToRegExp('/*/')).toEqual('^/(.*)$')
expect(convertToRegExp('/pt/*')).toEqual('^/pt/(.*)$')
})
})
Expand Down

0 comments on commit 6bb2331

Please sign in to comment.