Skip to content

Commit

Permalink
✨ Head タグが正しくビルドされるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
wappon28dev committed Jul 2, 2024
1 parent b277613 commit dca2007
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 54 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"bits-ui": "^0.21.2",
"cheerio": "^1.0.0-rc.12",
"highlight.js": "^11.9.0",
"just-extend": "^6.2.0",
"microcms-js-sdk": "^3.1.0",
"nanostores": "^0.10.2",
"the-new-css-reset": "^1.11.2"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/assets/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const INFO = {
title: "MISC - 愛工大名電 情報システム部",
description: "愛工大名電 情報システム部の公式サイトです。",
},
url: "https://mise-mdn.dev",
url: "https://misc-mdn.dev",
internal: {
id: "misc-mdn",
localStorageVersion: "1",
Expand Down
32 changes: 0 additions & 32 deletions src/lib/components/HeadTags.svelte

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/utils/types/microcms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type DateType = {
revisedAt: string;
};
type Structure<T, P> = T extends "get"
? { id: string } & DateType & Required<P>
? { id: string } & DateType & P
: T extends "gets"
? GetsType<{ id: string } & DateType & Required<P>>
? GetsType<{ id: string } & DateType & P>
: Partial<DateType> & (T extends "patch" ? Partial<P> : P);

export type Blogs<T = "get"> = Structure<
Expand Down
10 changes: 8 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script lang="ts">
import extend from "just-extend";
import { MetaTags } from "svelte-meta-tags";
import { setupViewTransition } from "sveltekit-view-transition";
import { page } from "$app/stores";
import Footer from "$lib/components/Footer.svelte";
import HeadTags from "$lib/components/HeadTags.svelte";
import Header from "$lib/components/Header.svelte";
import "the-new-css-reset";
Expand All @@ -12,6 +14,10 @@
import "$lib/styles/article.scss";
setupViewTransition();
export let data;
$: metaTags = extend(true, {}, data.baseMetaTags, $page.data.pageMetaTags);
</script>

<svelte:head>
Expand All @@ -26,7 +32,7 @@
</script>
</svelte:head>

<HeadTags />
<MetaTags {...metaTags} />

<div>
<Header />
Expand Down
39 changes: 39 additions & 0 deletions src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
import type { MetaTagsProps } from "svelte-meta-tags";
import type { LayoutLoad } from "./$types";
import { INFO } from "$lib/assets/info.js";

export const load: LayoutLoad = ({ url }) => {
const image = new URL("/images/ogp.png", INFO.url).href;

const baseMetaTags = Object.freeze({
title: INFO.about.title,
description: INFO.about.description,
canonical: new URL(url.pathname, url.origin).href,
openGraph: {
type: "website",
url: new URL(url.pathname, url.origin).href,
locale: "ja_JP",
title: INFO.about.title,
description: INFO.about.description,
siteName: INFO.about.title,
images: [
{
url: image,
width: 1200,
height: 630,
},
],
},
twitter: {
cardType: "summary_large_image",
title: INFO.about.title,
description: INFO.about.description,
image: image,
},
} as const satisfies MetaTagsProps);

return {
baseMetaTags,
};
};

export const prerender = true;
export const trailingSlash = "always";
4 changes: 0 additions & 4 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<script lang="ts">
import { onMount } from "svelte";
import { MetaTags } from "svelte-meta-tags";
import type { PageData } from "./$types";
import { INFO } from "$lib/assets/info";
import About from "$lib/components/About.svelte";
import Arrow from "$lib/components/Arrow.svelte";
import BlogList from "$lib/components/BlogList.svelte";
Expand All @@ -25,8 +23,6 @@
});
</script>

<MetaTags title={INFO.about.title} />

<div class="landing">
<p>夢が現実になる、<wbr />未来への一歩を</p>
<div class="arrow">
Expand Down
12 changes: 12 additions & 0 deletions src/routes/about/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { MetaTagsProps } from "svelte-meta-tags";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async () => {
const pageMetaTags = Object.freeze({
title: "About",
} as const satisfies MetaTagsProps);

return {
pageMetaTags,
};
};
3 changes: 0 additions & 3 deletions src/routes/about/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<script lang="ts">
import Icon from "@iconify/svelte";
import About from "$lib/components/About.svelte";
import HeadTags from "$lib/components/HeadTags.svelte";
import Marker from "$lib/components/Marker.svelte";
</script>

<HeadTags title="ABOUT" />

<h2>
<Marker>about</Marker>
</h2>
Expand Down
6 changes: 6 additions & 0 deletions src/routes/blogs/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import type { MetaTagsProps } from "svelte-meta-tags";
import type { PageServerLoad } from "./$types";
import { getContentList } from "$lib/utils/services/microcms";

export const load: PageServerLoad = async () => {
const res = await getContentList("blogs");

const pageMetaTags = Object.freeze({
title: "Blogs",
} as const satisfies MetaTagsProps);

return {
blogList: res,
pageMetaTags,
};
};
3 changes: 0 additions & 3 deletions src/routes/blogs/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<script lang="ts">
import type { PageData } from "./$types";
import BlogList from "$lib/components/BlogList.svelte";
import HeadTags from "$lib/components/HeadTags.svelte";
import Marker from "$lib/components/Marker.svelte";
export let data: PageData;
</script>

<HeadTags title="BLOGS" />

<h1>
<Marker>blogs</Marker>
</h1>
Expand Down
26 changes: 26 additions & 0 deletions src/routes/blogs/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import type { MetaTagsProps } from "svelte-meta-tags";
import type { PageServerLoad } from "./$types";
import { INFO } from "$lib/assets/info";
import { getContentDetail } from "$lib/utils/services/microcms";

export const load: PageServerLoad = async ({ params }) => {
const { slug } = params;
const res = await getContentDetail("blogs", slug);

const title = `${res.title} | Blogs | ${INFO.about.title}`;
const description = res.description ?? INFO.about.description;
const image = res.ogpImg?.url ?? new URL("/images/ogp.png", INFO.url).href;
const pageMetaTags = Object.freeze({
title,
openGraph: {
title,
description,
images: [
{
url: image,
},
],
},
twitter: {
cardType: "summary_large_image",
title,
description,
image,
},
} as const satisfies MetaTagsProps);

return {
detail: res,
pageMetaTags,
};
};
7 changes: 0 additions & 7 deletions src/routes/blogs/[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
<script lang="ts">
import type { PageData } from "./$types";
import BlogDetails from "$lib/components/BlogDetails.svelte";
import HeadTags from "$lib/components/HeadTags.svelte";
export let data: PageData;
</script>

<HeadTags
title={data.detail.title}
description={data.detail.description}
ogpImgUrl={data.detail.ogpImg?.url}
/>

<BlogDetails details={data.detail} />

0 comments on commit dca2007

Please sign in to comment.