From 5248e182fef7c5d6e2ad166d6743d27ad5e20301 Mon Sep 17 00:00:00 2001 From: Fonger <5862369+Fonger@users.noreply.github.com> Date: Wed, 15 Apr 2020 07:12:02 +0800 Subject: [PATCH] fix(export): fallback to empty string for basePath (#11880) * fix(next-server): fallback to empty string for basePath * Revert extra change and add unit test Co-authored-by: JJ Kasper --- packages/next/next-server/lib/router/router.ts | 2 +- test/unit/router-add-base-path.test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/unit/router-add-base-path.test.js diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index b7084450359bd..6fc5be2fb0cdc 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -16,7 +16,7 @@ import { isDynamicRoute } from './utils/is-dynamic' import { getRouteMatcher } from './utils/route-matcher' import { getRouteRegex } from './utils/route-regex' -const basePath = process.env.__NEXT_ROUTER_BASEPATH as string +const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || '' export function addBasePath(path: string): string { return path.indexOf(basePath) !== 0 ? basePath + path : path diff --git a/test/unit/router-add-base-path.test.js b/test/unit/router-add-base-path.test.js new file mode 100644 index 0000000000000..ab308dd20f578 --- /dev/null +++ b/test/unit/router-add-base-path.test.js @@ -0,0 +1,9 @@ +/* eslint-env jest */ +import { addBasePath } from 'next/dist/next-server/lib/router/router' + +describe('router addBasePath', () => { + it('should add basePath correctly when no basePath', () => { + const result = addBasePath('/hello') + expect(result).toBe('/hello') + }) +})