Skip to content

Commit 7bd1c85

Browse files
committed
Optimize static Nuxt generation
1 parent 7d7ba29 commit 7bd1c85

3 files changed

Lines changed: 38 additions & 61 deletions

File tree

‎components/Header.vue‎

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
1-
<script setup>
2-
import { onBeforeUnmount, onMounted, ref } from "vue";
3-
4-
const route = useRoute();
5-
const menu = ref(null);
6-
const menuButton = ref(null);
7-
const menuOpen = ref(false);
8-
9-
const closeMenu = () => {
10-
menuOpen.value = false;
11-
};
12-
13-
const handlePointerDown = (event) => {
14-
if (event.target instanceof Node && !menu.value?.contains(event.target)) {
15-
closeMenu();
16-
}
17-
};
18-
19-
const handleKeydown = (event) => {
20-
if (event.key === "Escape" && menuOpen.value) {
21-
closeMenu();
22-
menuButton.value?.focus();
1+
<script setup lang="ts">
2+
const closeMenu = (event: MouseEvent) => {
3+
if (event.currentTarget instanceof HTMLElement) {
4+
event.currentTarget.closest("details")?.removeAttribute("open");
235
}
246
};
25-
26-
onMounted(() => {
27-
document.addEventListener("pointerdown", handlePointerDown);
28-
document.addEventListener("keydown", handleKeydown);
29-
});
30-
31-
onBeforeUnmount(() => {
32-
document.removeEventListener("pointerdown", handlePointerDown);
33-
document.removeEventListener("keydown", handleKeydown);
34-
});
357
</script>
368

379
<template>
@@ -40,17 +12,10 @@ onBeforeUnmount(() => {
4012
<div class="flex items-center text-gray-700 dark:text-gray-200">
4113
<div class="flex-1" />
4214
<div class="flex items-center space-x-2">
43-
<div ref="menu" class="relative z-50 inline-block text-left">
44-
<div>
45-
<button
46-
ref="menuButton"
47-
type="button"
48-
aria-haspopup="true"
49-
:aria-expanded="menuOpen"
50-
aria-controls="site-navigation"
51-
class="transform align-middle text-gray-700 transition-all duration-300 ease-in-out hover:scale-110 hover:text-orange-500 active:scale-95 dark:text-white"
52-
@click="menuOpen = !menuOpen"
53-
>
15+
<details class="relative z-50 inline-block text-left">
16+
<summary
17+
class="transform list-none align-middle text-gray-700 transition-all duration-300 ease-in-out hover:scale-110 hover:text-orange-500 active:scale-95 [&::-webkit-details-marker]:hidden dark:text-white"
18+
>
5419
<svg
5520
xmlns="http://www.w3.org/2000/svg"
5621
viewBox="0 0 20 20"
@@ -63,19 +28,9 @@ onBeforeUnmount(() => {
6328
clip-rule="evenodd"
6429
/>
6530
</svg>
66-
</button>
67-
</div>
31+
</summary>
6832

69-
<Transition
70-
enter-active-class="transition duration-100 ease-out"
71-
enter-from-class="transform scale-95 opacity-0"
72-
enter-to-class="transform scale-100 opacity-100"
73-
leave-active-class="transition duration-75 ease-in"
74-
leave-from-class="transform scale-100 opacity-100"
75-
leave-to-class="transform scale-95 opacity-0"
76-
>
77-
<nav
78-
v-if="menuOpen"
33+
<nav
7934
id="site-navigation"
8035
aria-label="Primary navigation"
8136
class="ring-opacity-5 absolute right-0 mt-2 w-36 origin-top-right divide-y divide-gray-100 bg-white/90 text-gray-900 shadow-lg ring-1 ring-black backdrop-blur-sm focus:outline-none dark:divide-gray-100 dark:bg-black/80 dark:text-white"
@@ -222,8 +177,7 @@ onBeforeUnmount(() => {
222177
</a>
223178
</div>
224179
</nav>
225-
</Transition>
226-
</div>
180+
</details>
227181
</div>
228182
</div>
229183
</div>

‎components/section/Experiments.vue‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const filtered_links = computed(() => {
4343
v-for="link in filtered_links"
4444
:key="link.title"
4545
:to="link.link"
46+
:prefetch-on="{ interaction: true }"
4647
>
4748
<img
4849
class="h-64 w-full bg-gray-800 object-cover grayscale hover:grayscale-0"

‎nuxt.config.ts‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1-
import { writeFileSync, mkdirSync } from "fs";
1+
import { readdirSync, writeFileSync, mkdirSync } from "fs";
22
import { join, dirname } from "path";
33
import tailwindcss from "@tailwindcss/vite";
44

5+
const STATIC_ROUTES = [
6+
"/",
7+
"/about",
8+
"/blog",
9+
"/experiments",
10+
"/experiments/audio",
11+
"/experiments/chords",
12+
"/experiments/matrix",
13+
"/experiments/midi",
14+
"/talks",
15+
"/sitemap.xml",
16+
"/robots.txt",
17+
"/atom",
18+
];
19+
20+
const blogRoutes = readdirSync(join(process.cwd(), "content", "blog"))
21+
.filter((fileName) => fileName.endsWith(".md"))
22+
.map((fileName) => `/blog/${fileName.replace(/\.md$/, "")}`);
23+
524
// https://nuxt.com/docs/api/configuration/nuxt-config
625
export default defineNuxtConfig({
26+
ssr: true,
727
components: true,
828
css: ["~/assets/css/tailwind.css"],
929
modules: ["./modules/content", "@comark/nuxt"],
@@ -40,9 +60,11 @@ export default defineNuxtConfig({
4060
},
4161

4262
nitro: {
43-
// render server-side routes as static content
63+
// GitHub Pages serves this as static output. Explicit routes make the
64+
// generated site independent of the prerenderer's link crawler.
4465
prerender: {
45-
routes: ["/sitemap.xml", "/robots.txt", "/atom"],
66+
crawlLinks: false,
67+
routes: [...STATIC_ROUTES, ...blogRoutes],
4668
},
4769
},
4870

@@ -53,7 +75,7 @@ export default defineNuxtConfig({
5375
hooks: {
5476
// Generate HTML redirect files for `/articles/**` to `/blog/**`
5577
async close() {
56-
const { readdirSync, statSync, existsSync } = await import("fs");
78+
const { statSync, existsSync } = await import("fs");
5779
const distBlogPath = join(process.cwd(), "dist", "blog");
5880
const distArticlesPath = join(process.cwd(), "dist", "articles");
5981

0 commit comments

Comments
 (0)