Skip to content

Bug: Improve back navigation logic in ItemComponent#5170

Open
milanmajchrak wants to merge 1 commit intoDSpace:mainfrom
dataquest-dev:improve-back-navigation
Open

Bug: Improve back navigation logic in ItemComponent#5170
milanmajchrak wants to merge 1 commit intoDSpace:mainfrom
dataquest-dev:improve-back-navigation

Conversation

@milanmajchrak
Copy link
Copy Markdown
Contributor

@milanmajchrak milanmajchrak commented Feb 26, 2026

References

Original author: @amadulhaxxani

Description

Improves the "Back to results" button logic on item pages by adding session storage as a fallback for the previous URL. This ensures the back button works correctly across page refreshes and prevents stale navigation when users visit items from different contexts (e.g. homepage vs. collection browse).

Instructions for Reviewers

List of changes in this PR:

  • Added storeUrlInSession() and getUrlFromSession() generic helper methods to RouteService for persisting URLs in session storage
  • Updated ItemComponent to store the previous URL in session storage and fall back to it when the route-based previous URL is not a valid navigation target
  • Added /home to the list of allowed previous routes so the back button appears when navigating from the homepage
  • Extracted URL validation into a pickAllowedPrevious() helper method for cleaner logic
  • Simplified the back() method to use the stored URL directly instead of re-subscribing to the observable
  • Updated test stubs and mocks in route-service.stub.ts, publication.component.spec.ts, untyped-item.component.spec.ts, and item.component.spec.ts
  • Added new test cases for home URL back navigation and for prioritizing the current route URL over stale session storage

How to test:

  1. Go to a collection page and click on an item — you should see "Back to results"; clicking it returns you to the collection
  2. Refresh the item page — the "Back to results" button should still appear (session storage fallback)
  3. Navigate to the homepage, click an item from "What's New" — you should see "Back to results" pointing to the homepage
  4. Regression check: From a collection, open an item. Then go to the homepage and open a different item. The back button should now point to the homepage, not to the previous collection

Checklist

This checklist provides a reminder of what we are going to look for when reviewing your PR. You do not need to complete this checklist prior creating your PR (draft PRs are always welcome).
However, reviewers may request that you complete any actions in this list if you have not done so. If you are unsure about an item in the checklist, don't hesitate to ask. We're here to help!

  • My PR is created against the main branch of code (unless it is a backport or is fixing an issue specific to an older branch).
  • My PR is small in size (e.g. less than 1,000 lines of code, not including comments & specs/tests), or I have provided reasons as to why that's not possible.
  • My PR passes ESLint validation using npm run lint
  • My PR doesn't introduce circular dependencies (verified via npm run check-circ-deps)
  • My PR includes TypeDoc comments for all new (or modified) public methods and classes. It also includes TypeDoc for large or complex private methods.
  • My PR passes all specs/tests and includes new/updated specs or tests based on the Code Testing Guide.
  • My PR aligns with Accessibility guidelines if it makes changes to the user interface.
  • My PR uses i18n (internationalization) keys instead of hardcoded English text, to allow for translations.
  • My PR includes details on how to test it. I've provided clear instructions to reviewers on how to successfully test this fix or feature.
  • If my PR includes new libraries/dependencies (in package.json), I've made sure their licenses align with the DSpace BSD License based on the Licensing of Contributions documentation.
  • If my PR includes new features or configurations, I've provided basic technical documentation in the PR itself.
  • If my PR fixes an issue ticket, I've linked them together.

Copilot AI review requested due to automatic review settings February 26, 2026 13:40
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves “Back to results” navigation on item pages by persisting an allowed previous URL in session storage, so the back button remains correct across refreshes and different navigation entry points (including /home).

Changes:

  • Added generic session storage URL helpers to RouteService.
  • Updated ItemComponent to validate/store previous URLs and fall back to session storage when route history isn’t usable; expanded allowed previous routes to include /home.
  • Updated/added unit tests and stubs to cover the revised behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/app/core/services/route.service.ts Adds sessionStorage helpers for storing/retrieving URLs.
src/app/item-page/simple/item-types/shared/item.component.ts Stores/uses a validated previous URL with a session fallback; allows /home.
src/app/item-page/simple/item-types/shared/item.component.spec.ts Adds tests for /home back navigation and “prefer route over stale session” behavior.
src/app/core/testing/route-service.stub.ts Extends the RouteService stub with session helper methods for tests.
src/app/item-page/simple/item-types/publication/publication.component.spec.ts Updates RouteService mocks to include new session helper methods.
src/app/item-page/simple/item-types/untyped-item/untyped-item.component.spec.ts Updates RouteService mocks to include new session helper methods.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/app/core/services/route.service.ts
Comment thread src/app/item-page/simple/item-types/shared/item.component.ts
Comment thread src/app/core/services/route.service.ts
Comment thread src/app/core/services/route.service.ts
Comment thread src/app/core/testing/route-service.stub.ts
@alegontarz
Copy link
Copy Markdown

QA testing performed (no code review).

Works as expected:

  1. "Back to results" correctly navigates to the previous page — the button points to the most recent valid route.
  2. After page refresh, the button remains visible and works.

@alegontarz alegontarz moved this from 🙋 Needs Reviewers Assigned to 👀 Under Review in DSpace 10.0 Release Feb 27, 2026
@tdonohue tdonohue moved this from 👀 Under Review to 👍 Reviewer Approved in DSpace 10.0 Release Feb 27, 2026
@github-actions
Copy link
Copy Markdown

Hi @milanmajchrak,
Conflicts have been detected against the base branch.
Please resolve these conflicts as soon as you can. Thanks!

@tdonohue
Copy link
Copy Markdown
Member

@milanmajchrak : Could you resolve the merge conflict on this PR so that we are able to move this forward? Thanks!

@milanmajchrak milanmajchrak force-pushed the improve-back-navigation branch from 8eaff6c to c6f6ae2 Compare April 29, 2026 11:34
@milanmajchrak
Copy link
Copy Markdown
Contributor Author

@milanmajchrak : Could you resolve the merge conflict on this PR so that we are able to move this forward? Thanks!

@tdonohue Thank you for the reminder.

The merge conflicts have been resolved and the branch has been cleaned up into a single commit.
At this point, the only remaining issue is one failing unit-test job on tests (20.x), which appears to be flaky:
it passes locally (including repeated runs), and the tests (22.x) job passes in CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: 👍 Reviewer Approved

Development

Successfully merging this pull request may close these issues.

5 participants