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

404 page redirects fitting vanity paths to the long-form url #270

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions 404.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
</style>
</head>

<body>
<body hidden>
<header></header>
<main class="error">
<section>
Expand All @@ -179,12 +179,22 @@ <h1>TAKE A MULLIGAN</h1>
</main>
<footer></footer>
<script type="module">
import { createOptimizedPicture } from "./scripts/lib-franklin.js";
const imageUrl = "/media_10b8f0c21a329ee5ab764abf34d64a94de757ea2a.png";
const imageAlt = "Tiger Woods regretting a bad shot";
const pictureElement = createOptimizedPicture(imageUrl, imageAlt);
const imageContainer = document.querySelector(".error-image");
imageContainer.appendChild(pictureElement);
import { createOptimizedPicture } from "/scripts/lib-franklin.js";
import sitemap from "/import/tools/importer/data/sitemap.json" assert {type: 'json'}
Copy link
Collaborator

Choose a reason for hiding this comment

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

That doesn't work on firefox and other browsers see https://caniuse.com/mdn-javascript_statements_import_import_assertions_type_json

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

firefox should now be supported


const pathLocation = sitemap.find((obj) => Object.hasOwn(obj, window.location.pathname))
if (pathLocation) {
const drivePath = '/content-v2/' + pathLocation[window.location.pathname].replace('/content/golfdigest-com/en/', '').replace('.html', '')
window.location.assign(drivePath)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the user clicks on the back button, does he end up in an infinite loop in this case ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no, I have not found that to be the case

} else {
document.body.hidden = false

const imageUrl = "/media_10b8f0c21a329ee5ab764abf34d64a94de757ea2a.png";
const imageAlt = "Tiger Woods regretting a bad shot";
const pictureElement = createOptimizedPicture(imageUrl, imageAlt);
const imageContainer = document.querySelector(".error-image");
imageContainer.appendChild(pictureElement);
}
</script>
</body>
</html>
13 changes: 12 additions & 1 deletion scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ const LCP_BLOCKS = [...Object.values(ARTICLE_TEMPLATES), 'hero']; // add your LC

const range = document.createRange();

// getting real path, and adjusting canonical link to use the vanity path
const canonicalLinkTag = document.head.querySelector('link[rel="canonical"]');
if (canonicalLinkTag) {
const longPathMetadata = document.createElement('meta');
longPathMetadata.setAttribute('property', 'hlx:long-form-path');
Copy link
Collaborator

Choose a reason for hiding this comment

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

longPathMetadata.content = canonicalLinkTag?.href;
document.head.appendChild(longPathMetadata);
window.canonicalLocation = canonicalLinkTag.href;
canonicalLinkTag.href = window.location.href;
}

export function replaceLinksWithEmbed(block) {
const embeds = ['youtube', 'brightcove', 'instagram', 'ceros'];
block.querySelectorAll(embeds.map((embed) => `a:only-child[href*="${embed}"]`).join(',')).forEach((embedLink) => {
Expand Down Expand Up @@ -398,7 +409,7 @@ export function addFavIcons() {
`;

// Remove placeholder
document.head.querySelector('head link[rel="icon"]').remove();
document.head.querySelector('head link[rel="icon"]')?.remove();

// Add favicons
document.head.insertAdjacentHTML('beforeend', favicons);
Expand Down