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 all 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
31 changes: 24 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,29 @@ <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";

const display404 = () => {
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);
}

fetch('/import/tools/importer/data/sitemap.json').then(response => response.json()).then((sitemap) => {
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)
} else {
display404()
}
}).catch(error => {
display404()
})
</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