Skip to content

Commit

Permalink
fix: detect language error on /_next/data/ input (github#22419)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesurowiec authored Oct 26, 2021
1 parent 5100b55 commit e3074d9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion middleware/detect-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function getUserLanguage(browserLanguages) {

// determine language code from a path. Default to en if no valid match
export function getLanguageCodeFromPath(path) {
const maybeLanguage = path.split('/')[path.startsWith('/_next/data/') ? 4 : 1].slice(0, 2)
const maybeLanguage = (path.split('/')[path.startsWith('/_next/data/') ? 4 : 1] || '').slice(0, 2)
return languageCodes.includes(maybeLanguage) ? maybeLanguage : 'en'
}

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/detect-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ describe('detect-language - getLanguageCodeFromPath', () => {
expect(getLanguageCodeFromPath('/_next/data/development/ja/articles/foo')).toBe('ja')
})

test('should not error on /_next/data/ input', () => {
expect(getLanguageCodeFromPath('/_next/data/')).toBe('en')
})

test('should return for paths with an extension', () => {
expect(getLanguageCodeFromPath('/ja.json')).toBe('ja')
expect(getLanguageCodeFromPath('/_next/data/development/ja.json')).toBe('ja')
Expand Down

0 comments on commit e3074d9

Please sign in to comment.