Skip to content

Commit e51736c

Browse files
committed
Fix DynamicPage and useHead
1 parent 923035a commit e51736c

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

resources/js/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "./bootstrap";
88
import { createApp } from "vue";
99
import { createMemoryHistory, createRouter } from "vue-router";
1010
import { createPinia } from "pinia";
11+
import { createHead } from "@unhead/vue/client";
1112
import axiosPlugin from "./plugins/axios";
1213
import { VueQueryPlugin } from "@tanstack/vue-query";
1314
import AlertModalPlugin from "@/composables/useAlertModal.js";
@@ -21,6 +22,7 @@ import "../sass/next.css";
2122
import i18n from "./i18n/locales";
2223

2324
const app = createApp(App);
25+
const head = createHead();
2426
const pinia = createPinia();
2527

2628
Object.entries(import.meta.glob("./**/*.vue", { eager: true })).forEach(
@@ -40,6 +42,7 @@ app.provide("appConfig", window.appConfig);
4042
app.provide("appCaptcha", window.appCaptcha);
4143

4244
app.use(pinia)
45+
.use(head)
4346
.use(axiosPlugin)
4447
.use(router)
4548
.use(storePlugin)

resources/js/composables/useDynamicPage.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ref, computed, inject } from "vue";
22
import { useRoute } from "vue-router";
3-
import { useHead } from "@unhead/vue";
43

54
export function useDynamicPage() {
65
const route = useRoute();
@@ -72,17 +71,6 @@ export function useDynamicPage() {
7271
const description =
7372
pageData.value.meta_description ||
7473
extractDescription(pageData.value.content);
75-
76-
useHead({
77-
title: `${title} | Your App Name`,
78-
meta: [
79-
{ name: "description", content: description },
80-
{ property: "og:title", content: title },
81-
{ property: "og:description", content: description },
82-
{ property: "og:type", content: "article" },
83-
{ property: "og:url", content: window.location.href },
84-
],
85-
});
8674
};
8775

8876
const extractDescription = (content) => {

resources/js/pages/DynamicPage.vue

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,10 @@
162162
</template>
163163

164164
<script setup>
165-
import { onMounted, watch } from "vue";
165+
import { onMounted, watch, computed } from "vue";
166166
import { useRoute, useRouter } from "vue-router";
167167
import MainLayout from "@/layouts/FeedLayout.vue";
168+
import { useHead } from "@unhead/vue";
168169
import { useDynamicPage } from "@/composables/useDynamicPage";
169170
import {
170171
ExclamationTriangleIcon,
@@ -186,6 +187,44 @@ const {
186187
formatDate,
187188
} = useDynamicPage();
188189
190+
const pageDescription = computed(() => {
191+
if (pageData.value.meta_description) {
192+
return pageData.value.meta_description;
193+
}
194+
195+
if (!pageData.value.content) {
196+
return `Learn more about ${pageTitle.value} on Loops`;
197+
}
198+
199+
const tempDiv = document.createElement("div");
200+
tempDiv.innerHTML = pageData.value.content;
201+
202+
const text = tempDiv.textContent || tempDiv.innerText || "";
203+
204+
const cleanText = text.replace(/\s+/g, " ").trim();
205+
return cleanText.length > 160
206+
? cleanText.substring(0, 157) + "..."
207+
: cleanText;
208+
});
209+
210+
const pageUrl = computed(() => {
211+
return window.location.origin + route.fullPath;
212+
});
213+
214+
useHead({
215+
title: computed(() => `${pageTitle.value} | Loops`),
216+
meta: computed(() => [
217+
{ name: "description", content: pageDescription.value },
218+
{ property: "og:title", content: pageTitle.value },
219+
{ property: "og:description", content: pageDescription.value },
220+
{ property: "og:type", content: "article" },
221+
{ property: "og:url", content: pageUrl.value },
222+
{ name: "twitter:card", content: "summary" },
223+
{ name: "twitter:title", content: pageTitle.value },
224+
{ name: "twitter:description", content: pageDescription.value },
225+
]),
226+
});
227+
189228
watch(
190229
() => currentSlug.value,
191230
(newSlug, oldSlug) => {

0 commit comments

Comments
 (0)