Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing in of getCurrentPath option #745

Merged
merged 3 commits into from Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,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
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