Skip to content

Commit

Permalink
Merge pull request #631 from dzcode-io/fix/canonical-url
Browse files Browse the repository at this point in the history
  • Loading branch information
ZibanPirate authored Dec 31, 2024
2 parents 175db46 + 106cd7c commit 99217d5
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion web/cloudflare/handler/contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export const handleContributionRequest: PagesFunction<Env> = async (context) =>
const newData = htmlTemplate
.replace(/{{template-title}}/g, pageTitle)
.replace(/{{template-description}}/g, localize("contribute-description"))
.replace(/{{template-lang}}/g, language);
.replace(/{{template-lang}}/g, language)
.replace(/{{template-canonical}}/g, `${fullstackConfig.web.url}${pathName}`);

return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } });
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion web/cloudflare/handler/contributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export const handleContributorRequest: PagesFunction<Env> = async (context) => {
const newData = htmlTemplate
.replace(/{{template-title}}/g, pageTitle)
.replace(/{{template-description}}/g, localize("team-description"))
.replace(/{{template-lang}}/g, language);
.replace(/{{template-lang}}/g, language)
.replace(/{{template-canonical}}/g, `${fullstackConfig.web.url}${pathName}`);

return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } });
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion web/cloudflare/handler/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export const handleProjectRequest: PagesFunction<Env> = async (context) => {
const newData = htmlTemplate
.replace(/{{template-title}}/g, pageTitle)
.replace(/{{template-description}}/g, localize("projects-description"))
.replace(/{{template-lang}}/g, language);
.replace(/{{template-lang}}/g, language)
.replace(/{{template-canonical}}/g, `${fullstackConfig.web.url}${pathName}`);

return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } });
} catch (error) {
Expand Down
6 changes: 6 additions & 0 deletions web/src/_build/gen-multiple-htmls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { readFileSync, writeFileSync, mkdirSync } from "fs";
import { allPages } from "./pages";
import { Environment, environments } from "@dzcode.io/utils/dist/config/environment";
import { SENTRY_ORIGIN } from "../../src/utils/sentry-origin";
import { fsConfig } from "@dzcode.io/utils/dist/config";

let stage = process.env.STAGE as Environment;
if (!environments.includes(stage)) {
console.log(`⚠️ No STAGE provided, falling back to "development"`);
stage = "development";
}

const config = fsConfig(stage);
const distFolder = "./bundle";

const indexHtmlPath = join(distFolder, "index.html");
Expand Down Expand Up @@ -56,6 +58,10 @@ allPages.forEach((pageInfo) => {
newHtml = newHtml.replace(/{{keywords}}/g, pageInfo.keywords);
newHtml = newHtml.replace(/{{title}}/g, pageInfo.title);
newHtml = newHtml.replace(/{{description}}/g, pageInfo.description);
newHtml = newHtml.replace(
/{{canonical}}/g,
pageInfo.canonicalUrl || `${config.web.url}${pageInfo.uri}`,
);
newHtml = newHtml.replace(/{{ogImage}}/g, pageInfo.ogImage);
newHtml = newHtml.replace(/{{sentryOrigin}}/g, `https://${SENTRY_ORIGIN}`);

Expand Down
1 change: 1 addition & 0 deletions web/src/_build/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { templatePages } from "./template-pages";

export interface PageInfo {
uri: string;
canonicalUrl?: string;
title: string;
description: string;
ogImage: string;
Expand Down
1 change: 1 addition & 0 deletions web/src/_build/pages/template-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export const templatePages: PageInfo[] = [
"https://images.unsplash.com/photo-1527285341945-715b98b98ea2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1200&h=627&q=80",
keywords: "open-source, algeria, dzcode",
lang: "{{template-lang}}" as unknown as PageInfo["lang"],
canonicalUrl: "{{template-canonical}}",
},
];
3 changes: 3 additions & 0 deletions web/src/_entry/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<title>{{title}}</title>
<meta name="description" content="{{description}}" />

<!-- SEO -->
<link rel="canonical" href="{{canonical}}" />

<!-- Facebook Meta Tags -->
<meta property="og:type" content="website" />
<meta property="og:title" content="{{title}}" />
Expand Down
1 change: 0 additions & 1 deletion web/src/pages/contribute/contribution/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default function Page(): JSX.Element {
{localize("contribution-title-post")}
</title>
<meta name="description" content={localize("contribute-description")} />
{/* @TODO-ZM: add canonical url on all pages */}
<link rel="canonical" href={getContributionURL(contribution)} />
</Helmet>
) : null}
Expand Down
1 change: 0 additions & 1 deletion web/src/pages/contribute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default function Page(): JSX.Element {
dispatch(fetchContributionsListAction());
}, [dispatch]);

// @TODO-ZM: add filters and search
return (
<main className="flex flex-col self-center">
<Helmet>
Expand Down
1 change: 0 additions & 1 deletion web/src/pages/projects/project/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default function Page(): JSX.Element {
{localize("project-title-pre")} {project.name} {localize("project-title-post")}
</title>
<meta name="description" content={localize("projects-description")} />
{/* @TODO-ZM: add canonical url on all pages */}
<link rel="canonical" href={getProjectURL(project)} />
</Helmet>
) : null}
Expand Down
1 change: 0 additions & 1 deletion web/src/utils/contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ export function getContributionURL({
id,
title,
}: Pick<ContributionEntity, "id" | "title">): string {
// @TODO-ZM: use slug instead
return `/contribute/${title.replace(/\s/g, "-")}-${id}`;
}

0 comments on commit 99217d5

Please sign in to comment.