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

EES 5633 - FE - Redirect the user to the latest version of a page if using an old Release slug in the URL #5419

Merged
merged 12 commits into from
Dec 16, 2024

Conversation

jack-hive
Copy link
Collaborator

@jack-hive jack-hive commented Nov 27, 2024

⚠️ This PR depends on some more BE changes we need to make on top of those in #5391. The new changes will be made in another PR under EES-5632. Please see #5446 to see those BE changes. [THIS PR IS BLOCKED BY #5446 ]⚠️

This PR consists of the frontend changes necessary to allow Release redirects to work.

Previously, we only had the ability to be able to redirect the user when we detected an old methodology/publication slug in the URL. But now, with the introduction of ReleaseRedirects in EES-5631 & EES-5632, we need to be able to handle redirects for releases too.

During this work, I had to refactor how the redirects worked on the frontend. Previously, we just looked for a prefix of either /methodology or /find-statistics in the URL and then checked if the next segment of the URL contained an old slug for either a methodology or publication, respectively, and then proceeded to redirect ANY child route.

However, this no longer works with the introduction of release redirects, because the current pages which contain a release slug in their URL also contain a publication slug. For this reason, if we only checked for the occurrence of a prefix in the URL before redirecting the user on any child route, then the release slug wouldn't be taken into account if we found a redirect for the publication. Equally, if we introduce any further routes in the future which contain multiple possible redirect slugs in their URL, they will be impacted too.

For this reason, I have refactored the redirect logic to only check for very explicit patterns in the URL, when trying to determine whether or not the URL looks like one that can be redirected. I've split out the 'URL Patterns' into a list, which can be iterated through and easily added to. We use the URL Pattern Standard to implement the matching logic, and use Regex to check for the various valid alternatives for a pattern (including checking for trailing slashes, which should be allowed). I have intentionally only included existing routes that need to be checked for redirects, and have not included subsequent child-routes that don't exist yet. Any new routes in the future, that have the potential to be redirected, can easily be added to the list of URL Patterns we check against.

Every (current) possible URL Pattern has been tested via Jest unit tests.

Other Related Changes


  • Previously, the unit tests were importing and using the same instance of the redirectPages module throughout each test. However, due to the redirectPages module containing state in the form of the cached redirects (see the cachedRedirects property), this was being carried over between tests and potentially interfering. This hasn't been detected until now, because the previous tests, although mocking the response from the redirectService.list method, always returned the same mocked data from that method. So, the caching had no consequence to whether the test passed or failed. Now we have multiple test cases for each test, corresponding to each unique URL Pattern, it was necessary to use Jest's isolateModulesAsync to isolate specific modules for every test so that local module state doesn't conflict between tests (which was being flagged when doing the expect(redirectService.list).toHaveBeenCalledTimes(1); checks in the tests)

@jack-hive jack-hive changed the title EES 5663 - FE - Redirect the user to the latest version of a page if using an old Release slug in the URL EES 5633 - FE - Redirect the user to the latest version of a page if using an old Release slug in the URL Nov 28, 2024
@jack-hive jack-hive added the do not merge Don't merge just yet label Dec 3, 2024
Copy link
Collaborator

@benoutram benoutram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it pretty tricky to review all of the different permutations that need to be tested but they seem to have all been covered and fall into the right categories! @jack-hive I'll just wait for you to review the additional comments I left before approving...

@jack-hive jack-hive force-pushed the EES-5633 branch 2 times, most recently from 1277614 to 44a46be Compare December 11, 2024 12:38
@jack-hive jack-hive changed the base branch from dev to EES-5632-distinguishing-release-redirects-across-different-publications December 11, 2024 14:52
@jack-hive jack-hive force-pushed the EES-5632-distinguishing-release-redirects-across-different-publications branch from 56cf3c6 to eb5ac48 Compare December 12, 2024 15:28
Base automatically changed from EES-5632-distinguishing-release-redirects-across-different-publications to dev December 12, 2024 15:49
… and to allow us to define more accurately which routes we expect redirects for
…re we weren't isolating a module between test runs

The module state (cache) of the `redirectPages` module was interfering with subsequent runs of the tests. So we have used Jest's built-in `isolateModulesAsync` method to ensure that the module state is not carried over between test runs.
…he redirect patterns + organising tests into groups
Was previously getting this error when running `pnpm run build` in the frontend project:

```
Failed to compile.

./..\..\node_modules\.pnpm\[email protected]\node_modules\lodash\lodash.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation

Import trace for requested module:
  ./..\..\node_modules\.pnpm\[email protected]\node_modules\lodash\lodash.js
  ./src\middleware\pages\redirectPages.ts
  ./src\middleware.ts
```
@jack-hive jack-hive merged commit dee7e0c into dev Dec 16, 2024
8 checks passed
@jack-hive jack-hive deleted the EES-5633 branch December 16, 2024 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge Don't merge just yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants