Skip to content

Commit

Permalink
Expose basePath as router field
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jan 8, 2020
1 parent 895e41f commit ce68478
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const nextServerlessLoader: loader.Loader = function() {
canonicalBase: "${canonicalBase}",
buildId: "${buildId}",
assetPrefix: "${assetPrefix}",
basePath: "${basePath}",
..._renderOpts
}
let sprData = false
Expand Down
9 changes: 8 additions & 1 deletion packages/next/client/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const singletonRouter: SingletonRouterBase = {
}

// Create public properties and methods of the router in the singletonRouter
const urlPropertyFields = ['pathname', 'route', 'query', 'asPath', 'components']
const urlPropertyFields = [
'pathname',
'route',
'query',
'asPath',
'components',
'basePath',
]
const routerEvents = [
'routeChangeStart',
'beforeHistoryChange',
Expand Down
1 change: 1 addition & 0 deletions packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export default async function(
dev: false,
staticMarkup: false,
hotReloader: null,
basePath: nextConfig.experimental.basePath,
canonicalBase: nextConfig.amp?.canonicalBase || '',
isModern: nextConfig.experimental.modern,
ampValidator: nextConfig.experimental.amp?.validator || undefined,
Expand Down
4 changes: 4 additions & 0 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type BaseRouter = {
pathname: string
query: ParsedUrlQuery
asPath: string
basePath: string
}

export type NextRouter = BaseRouter &
Expand Down Expand Up @@ -73,6 +74,8 @@ export default class Router implements BaseRouter {
pathname: string
query: ParsedUrlQuery
asPath: string
basePath: string

/**
* Map of all components loaded in `Router`
*/
Expand Down Expand Up @@ -138,6 +141,7 @@ export default class Router implements BaseRouter {
this.asPath =
// @ts-ignore this is temporarily global (attached to window)
isDynamicRoute(pathname) && __NEXT_DATA__.autoExport ? pathname : as
this.basePath = basePath
this.sub = subscription
this.clc = null
this._wrapApp = wrapApp
Expand Down
2 changes: 2 additions & 0 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default class Server {
documentMiddlewareEnabled: boolean
hasCssMode: boolean
dev?: boolean
basePath: string
}
private compression?: Middleware
private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
Expand Down Expand Up @@ -148,6 +149,7 @@ export default class Server {
staticMarkup,
buildId: this.buildId,
generateEtags,
basePath: this.nextConfig.experimental.basePath,
}

// Only the `publicRuntimeConfig` key is exposed to the client side
Expand Down
13 changes: 11 additions & 2 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,22 @@ class ServerRouter implements NextRouter {
pathname: string
query: ParsedUrlQuery
asPath: string
basePath: string
events: any
// TODO: Remove in the next major version, as this would mean the user is adding event listeners in server-side `render` method
static events: MittEmitter = mitt()

constructor(pathname: string, query: ParsedUrlQuery, as: string) {
constructor(
pathname: string,
query: ParsedUrlQuery,
as: string,
basePath: string
) {
this.route = pathname.replace(/\/$/, '') || '/'
this.pathname = pathname
this.query = query
this.asPath = as
this.basePath = basePath
}
push(): any {
noRouter()
Expand Down Expand Up @@ -155,6 +162,7 @@ type RenderOpts = {
revalidate?: number | boolean
}
unstable_getStaticPaths?: () => void
basePath: string
}

function renderDocument(
Expand Down Expand Up @@ -272,6 +280,7 @@ export async function renderToHTML(
ErrorDebug,
unstable_getStaticProps,
unstable_getStaticPaths,
basePath,
} = renderOpts

const callMiddleware = async (method: string, args: any[], props = false) => {
Expand Down Expand Up @@ -369,7 +378,7 @@ export async function renderToHTML(

// @ts-ignore url will always be set
const asPath: string = req.url
const router = new ServerRouter(pathname, query, asPath)
const router = new ServerRouter(pathname, query, asPath, basePath)
const ctx = {
err,
req: isAutoExport ? undefined : req,
Expand Down
14 changes: 9 additions & 5 deletions test/integration/basepath/pages/hello.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import Link from 'next/link'
import { useRouter } from 'next/router'

export default () => (
<Link href="/other-page">
<a id="other-page-link">
<h1>Hello World</h1>
</a>
</Link>
<>
<Link href="/other-page">
<a id="other-page-link">
<h1>Hello World</h1>
</a>
</Link>
<div id="base-path">{useRouter().basePath}</div>
</>
)
6 changes: 6 additions & 0 deletions test/integration/basepath/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ const runTests = (context, dev = false) => {
}
})

it('should have basePath field on Router', async () => {
const html = await renderViaHTTP(context.appPort, '/docs/hello')
const $ = cheerio.load(html)
expect($('#base-path').text()).toBe('/docs')
})

it('should navigate to the page without refresh', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
try {
Expand Down

0 comments on commit ce68478

Please sign in to comment.