Skip to content

Commit

Permalink
feat: allow passing in of getCurrentPath option
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnaish committed Feb 4, 2020
1 parent b296b36 commit 08505e5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/dotcom-middleware-navigation/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ The middleware accepts the following parameters. All options will be passed alon

Enables fetching hierarchical navigation data for the current path including any parent and child pages. Defaults to `false`.

### `getCurrentPath`

Enables overriding of the default current path logic. Defaults to:

```js
(request) => normalizePath(request.get('ft-vanity-url') || request.path)
```

### `interval`

See the [FT navigation documentation] for more details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,36 @@ describe('dotcom-middleware-navigation', () => {
})
})

describe('when handling a request with a custom getCurrentPath', () => {
it('executes the provided getCurrentPath function', async () => {
request = httpMocks.createRequest({
path: '/path/to/page/123',
headers: {
'ft-vanity-url': '/vanity-url?page=2'
}
})
const dummyPath = '/foo'
const instance = subject.init({ getCurrentPath: () => dummyPath })
await instance(request, response, next)

expect(FakeNavigation.getSubNavigationFor).toHaveBeenCalledWith(dummyPath)
})

it('allows overriding of how to calculate current path logic', async () => {
request = httpMocks.createRequest({
path: '/path/to/page/123',
headers: {
'ft-blocked-url': '/ig-content-test'
}
})

const instance = subject.init({ getCurrentPath: (request) => request.get('ft-blocked-url') })
await instance(request, response, next)

expect(FakeNavigation.getSubNavigationFor).toHaveBeenCalledWith('/ig-content-test')
})
})

describe('when something goes wrong', () => {
beforeEach(() => {
FakeNavigation.getMenusFor = jest.fn(() => {
Expand Down
6 changes: 4 additions & 2 deletions packages/dotcom-middleware-navigation/src/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import normalizePath from './normalizePath'

type MiddlewareOptions = TNavOptions & {
enableSubNavigation?: boolean
getCurrentPath?: Function
}

const defaultOptions: MiddlewareOptions = {
enableSubNavigation: false
enableSubNavigation: false,
getCurrentPath: (request) => normalizePath(request.get('ft-vanity-url') || request.path)
}

export const init = (userOptions: MiddlewareOptions = {}) => {
Expand All @@ -26,7 +28,7 @@ export const init = (userOptions: MiddlewareOptions = {}) => {
// rather than the underlying path, so prefer that when available.
// <https://github.com/Financial-Times/ft.com-cdn/blob/4841fbf100e1c561a2f6729b9921ec12bb6b837c/src/vcl/next-preflight.vcl#L213-L219>
// NOTE: Next router sets the vanity header inc. any query string so it must be normalized.
const currentPath = normalizePath(request.get('ft-vanity-url') || request.path)
const currentPath = options.getCurrentPath(request)
const currentEdition = handleEdition(request, response)

const [menusData, subNavigationData] = await Promise.all([
Expand Down

0 comments on commit 08505e5

Please sign in to comment.