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

Experiment: Page enhancement allowing head elements to be added for a given page and automatically removed when routing away #62

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

bayes343
Copy link
Contributor

@bayes343 bayes343 commented Jun 19, 2024

Background

Outside of updating title and meta tags, which are handled via the SeoService and Page properties, modifications to the head need to be handled via custom code. This branch is an experiment if providing a Page level configuration option for declaring elements that should be rendered in the head for that page, but not retained after routing away from that page.

Use case

  • You have a custom script, stylesheet, etc. that should or needs to be loaded only on certain routes.

Implementation

  • A new Page property has been added called, "HeaderElements"
  • This new property is an array of Jsx - elements you want added to the head for that page
  • Before appending them to the head on a positive route match, a new attribute (currently "dynamic") is added with a value of true
  • When the app routes to another page, it removes the elements added to the page by querying for that new attribute

Example usage

  • In addition to tests, see below snippets/screenshots from a working example
export class NotFoundPage extends Page {
  Title = 'Not Found';

  RenderMode = RenderModes.Dynamic;
  Route = async () => true;
  HeadElements = [
    <script src="/hello.js" async="true" />
  ];

^ script from above "hello.js" executes, alert('hello world');

  1. Initial load on homepage (no hello script)
image
  1. After client or non-client routing to 404 page (script is added and executes)
image
  1. After client or non-client routing back to the home page
image

@bayes343 bayes343 requested a review from sdapalsch June 19, 2024 03:46
Copy link

Code Coverage

Package Line Rate Branch Rate Complexity Health
components 100% 100% 0
core 100% 100% 0
core.decorators 100% 100% 0
core.services 100% 100% 0
core.services.router 100% 100% 0
core.services.seoService 100% 100% 0
core.services.xssSanitizerService 100% 100% 0
utilities 100% 100% 0
Summary 100% (513 / 513) 100% (175 / 175) 0

@bayes343
Copy link
Contributor Author

Moved the logic related to this to the component level, allowing usage at the component or page level.

The elements added to the head are removed once the component is removed from the Dom.

Before potentially merging this feature, it needs to be adjusted to be fully await able, such that the elements are added to the head before the render method returns.

Copy link

sonarcloud bot commented Jul 27, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants