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 6135831
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/dotcom-middleware-navigation/readme.md
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
Expand Up @@ -136,6 +136,43 @@ describe('dotcom-middleware-navigation', () => {
})
})

describe('when handling a request with a custom getCurrentPath', () => {

beforeEach(() => {
instance = subject.init({ getCurrentPath: () => dummyPath })
})

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
8 changes: 5 additions & 3 deletions packages/dotcom-middleware-navigation/src/navigation.ts
Expand Up @@ -5,11 +5,13 @@ import handleEdition from './handleEdition'
import normalizePath from './normalizePath'

type MiddlewareOptions = TNavOptions & {
enableSubNavigation?: boolean
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 6135831

Please sign in to comment.