-
Notifications
You must be signed in to change notification settings - Fork 258
WS-1400: Add client side redirect #13407
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
Draft
shayneahchoon
wants to merge
27
commits into
latest
Choose a base branch
from
WS-1400-ECT-HELMET
base: latest
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+163
−19
Draft
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
493f7bd
WS-1400: Add client side redirect
shayneahchoon 717d0bb
Merge branch 'latest' into WS-1400-ECT-HELMET
shayneahchoon 01d4e4d
Merge branch 'latest' into WS-1400-ECT-HELMET
shayneahchoon c994aba
WS-1533: Update packages with 7 day maturity threshold
shayneahchoon 2bea896
Merge branch 'WS-1400-ECT-HELMET' of github.com:bbc/simorgh into WS-1…
shayneahchoon 70e7ad7
Merge branch 'latest' into WS-1400-ECT-HELMET
shayneahchoon 6359b3a
WS-1533: Update packages with 7 day maturity threshold
shayneahchoon 15f88f5
Merge branch 'WS-1400-ECT-HELMET' of github.com:bbc/simorgh into WS-1…
shayneahchoon d5c84a4
WS-1533: Update packages with 7 day maturity threshold
shayneahchoon 9bac75e
WS-1533: Update packages with 7 day maturity threshold
shayneahchoon 1e086de
WS-1399: Add client side redirect
shayneahchoon 4cdf217
Merge branch 'latest' into WS-1400-ECT-HELMET
shayneahchoon f01ad51
WS-1399: Add client side redirect
shayneahchoon a606199
Merge branch 'WS-1400-ECT-HELMET' of github.com:bbc/simorgh into WS-1…
shayneahchoon 4dfd6cc
WS-1399: Add client side redirect
shayneahchoon 6832436
WS-1399: Add client side redirect
shayneahchoon dc3594d
WS-1399: Add client side redirect
shayneahchoon aa80aaf
WS-1399: Add client side redirect
shayneahchoon 2a2be1c
WS-1399: Add client side redirect
shayneahchoon e0dbde9
WS-1399: Add client side redirect
shayneahchoon 5f2df6e
WS-1399: Add client side redirect
shayneahchoon 97dfa11
WS-1399: Add client side redirect
shayneahchoon 48451c3
WS-1399: Add client side redirect
shayneahchoon 1571678
Merge branch 'latest' into WS-1400-ECT-HELMET
shayneahchoon 83a1a3b
WS-1737: Add tracking for redirect
shayneahchoon 657b4c2
Merge branch 'WS-1400-ECT-HELMET' of github.com:bbc/simorgh into WS-1…
shayneahchoon 501c8cc
WS-1737: Add tracking for redirect
shayneahchoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { redirectScript } from '.'; | ||
|
|
||
| describe('LiteRedirect', () => { | ||
| it.each([ | ||
| { | ||
| effectiveType: 'randomValue', | ||
| expectedRedirect: false, | ||
| }, | ||
| { | ||
| effectiveType: 'slow-2g', | ||
| expectedRedirect: true, | ||
| }, | ||
| { | ||
| effectiveType: '2g', | ||
| expectedRedirect: true, | ||
| }, | ||
| { | ||
| effectiveType: '3g', | ||
| expectedRedirect: true, | ||
| }, | ||
| { | ||
| effectiveType: '4g', | ||
| expectedRedirect: false, | ||
| }, | ||
| ])( | ||
| `When the client is on $effectiveType then expect redirect should be $expectRedirect`, | ||
| ({ effectiveType, expectedRedirect }) => { | ||
| const mockWindow = { | ||
| navigator: { | ||
| connection: { | ||
| effectiveType, | ||
| }, | ||
| }, | ||
| location: { | ||
| replace: jest.fn(), | ||
| href: 'https://www.somepath.com/', | ||
| pathname: '/pidgin/articles/czrzwn80zjmo', | ||
| }, | ||
| } as unknown as Window; | ||
|
|
||
| redirectScript(mockWindow); | ||
| const replaceCallStack = (mockWindow.location.replace as jest.Mock).mock | ||
| .calls[0]?.[0]; | ||
|
|
||
| const hasRedirected = Boolean(replaceCallStack); | ||
|
|
||
| expect(hasRedirected).toBe(expectedRedirect); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(['.amp', '.lite'])('should not redirect for %s', type => { | ||
| const mockWindow = { | ||
| navigator: { | ||
| connection: { | ||
| effectiveType: '5g', | ||
| }, | ||
| }, | ||
| location: { | ||
| replace: jest.fn(), | ||
| href: `https://www.somepath.com${type}`, | ||
| }, | ||
| } as unknown as Window; | ||
|
|
||
| redirectScript(mockWindow); | ||
| const replaceCallStack = (mockWindow.location.replace as jest.Mock).mock | ||
| .calls[0]?.[0]; | ||
|
|
||
| const hasRedirected = Boolean(replaceCallStack); | ||
|
|
||
| expect(hasRedirected).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import React from 'react'; | ||
| import { Helmet } from 'react-helmet'; | ||
|
|
||
| export const redirectScript = (window: Window) => { | ||
| const { pathname, href } = window.location; | ||
| const isLite = /\.lite$/.test(href); | ||
| const isAmp = /\.amp$/.test(href); | ||
|
|
||
| const allowList = ['/pidgin/articles/czrzwn80zjmo']; | ||
|
|
||
| if ( | ||
| !isLite && | ||
| !isAmp && | ||
| window?.navigator?.connection?.effectiveType && | ||
| allowList.includes(pathname) | ||
| ) { | ||
| const toLitePath = `${pathname}.lite`; | ||
| const ect = window.navigator.connection.effectiveType; | ||
| const normalisedEct = ect.toLocaleLowerCase(); | ||
| switch (normalisedEct) { | ||
| case 'slow-2g': | ||
| case '2g': | ||
| case '3g': | ||
| window.location.replace(toLitePath); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export default () => { | ||
| const innerHTML = `( | ||
| window.addEventListener('load', () => { | ||
| (${redirectScript.toString()})(window) | ||
| }) | ||
| )`; | ||
|
|
||
| return ( | ||
| <Helmet | ||
amoore108 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| script={[ | ||
| { | ||
| type: 'text/javascript', | ||
| innerHTML, | ||
| }, | ||
| ]} | ||
| /> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.