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

Fix news OpenGraph not working #92

Merged
merged 6 commits into from
Oct 6, 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
10 changes: 3 additions & 7 deletions src/lib/ArticlePreview.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<script lang="ts">
import Button from "$lib/Button.svelte";
import { env } from "$env/dynamic/public";
import { logo } from "$lib/logo.js";
import { seoAutofillImage } from "$lib/seoAutofillImage";

export let preview;
</script>

<div class="container">
{#if preview.postImage}
<img class="preview-image" loading="lazy" src="{env.PUBLIC_SERVER_URL + preview.postImage.url}" alt="{preview.postImage.alt}"/>
<img class="preview-image" loading="lazy" src="{env.PUBLIC_SERVER_URL + preview.postImage.url}" alt="{preview.postImage.alt}">
{:else}
<img class="preview-image logo" loading="lazy" src="{env.PUBLIC_SERVER_URL + $logo.url}" alt="{$logo.alt}">
<img class="preview-image" loading="lazy" src="{env.PUBLIC_SERVER_URL + $seoAutofillImage.url}" alt="{$seoAutofillImage.alt}">
{/if}
<div class="content">
<div class="text">
Expand All @@ -37,10 +37,6 @@
width: 100%;
}

.preview-image.logo {
object-fit: contain;
}

.content {
display: flex;
padding: 1rem;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/BigArticlePreview.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import Button from "$lib/Button.svelte";
import { env } from "$env/dynamic/public";
import { logo } from "$lib/logo";
import { seoAutofillImage } from "$lib/seoAutofillImage";

export let preview;
</script>
Expand All @@ -10,7 +10,7 @@
{#if preview.postImage}
<img class="preview-image" loading="lazy" src="{env.PUBLIC_SERVER_URL + preview.postImage.url}" alt="{preview.postImage.alt}">
{:else}
<img class="preview-image logo" loading="lazy" src="{env.PUBLIC_SERVER_URL + $logo.url}" alt="{$logo.alt}">
<img class="preview-image logo" loading="lazy" src="{env.PUBLIC_SERVER_URL + $seoAutofillImage.url}" alt="{$seoAutofillImage.url}">
{/if}
<div class="content">
<div class="text">
Expand Down Expand Up @@ -39,11 +39,11 @@
width: 100%;
height: 100%;
bottom: 0;
filter: brightness(0.5);;
filter: brightness(0.5);
}

.preview-image.logo {
object-fit: contain;
background-color: #FFFFFF;
}

.content {
Expand Down
5 changes: 0 additions & 5 deletions src/lib/logo.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/lib/seoAutofillImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { writable } from "svelte/store";

const seoAutofillImage = writable({});

export { seoAutofillImage };
2 changes: 2 additions & 0 deletions src/routes/+layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export async function load({ fetch, data, url }) {

return {
seoData: remoteSeoData,
pathname: url.pathname,
href: url.href,
...data,
};
}
7 changes: 7 additions & 0 deletions src/routes/+layout.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export async function load({ fetch }) {
alt
width
}
seoAutofillImage {
url
alt
}
favicon {
url
}
favicons {
size
favicon {
Expand Down
63 changes: 31 additions & 32 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import { cacheExchange, Client, fetchExchange, setContextClient } from "@urql/svelte";
import { env } from "$env/dynamic/public";
import { seoInfo } from "../lib/seoInfo";
import { page } from "$app/stores";
import { logo } from "$lib/logo";
import { seoAutofillImage } from "$lib/seoAutofillImage";

setContext('layout', layout);

Expand All @@ -29,7 +28,7 @@
import('@fontsource/alegreya/500.css');
import('@fontsource/alegreya/700.css');

logo.set(data.MainInfo.logo)
seoAutofillImage.set(data.MainInfo.seoAutofillImage)

layout.subscribe((layoutProps) => {
window.document.body.style.backgroundColor = layoutProps.navbar.backgroundColor
Expand All @@ -43,43 +42,43 @@

setContextClient(client);

seoInfo.set({
title: data.MainInfo.name,
description: undefined,
url: $page.url.href,
siteName: data.MainInfo.name,
imageUrl: undefined,
type: undefined,
publishDate: undefined
})
const resetSeo = () => {
seoInfo.set({
title: data.MainInfo.name,
description: data.MainInfo.name,
url: data.href,
siteName: data.MainInfo.name,
imageUrl: data.MainInfo.seoAutofillImage.url,
type: "website",
publishDate: undefined
})
}

resetSeo()

$: if (data.seoData) {
seoInfo.update((seoInfo) => {
seoInfo.title = data.seoData.title
seoInfo.description = data.seoData.description
seoInfo.imageUrl = data.seoData.image.url
seoInfo.url = $page.url.href
seoInfo.type = undefined
seoInfo.publishDate = undefined

return seoInfo
seoInfo.update((seo) => {
seo.title = data.seoData.title
seo.description = data.seoData.description
seo.imageUrl = data.seoData.image ? data.seoData.image.url: data.MainInfo.seoAutofillImage.url
seo.url = data.href
seo.type = "website"
seo.publishDate = undefined

if (data.pathname.startsWith("/news/")) {
seo.type = "article"
seo.publishDate = data.seoData.lastUpdate
}

return seo
})
} else {
if (!$page.url.pathname.startsWith("/news/")) {
seoInfo.set({
title: data.MainInfo.name,
description: undefined,
url: $page.url.href,
siteName: data.MainInfo.name,
imageUrl: undefined,
type: undefined,
publishDate: undefined
})
}
resetSeo()
}
</script>

<svelte:head>
<link rel="icon" type="image/x-icon" href="/favicon.ico">
{#each data.MainInfo.favicons as favicon}
<link rel="icon" type="image/png" sizes="{favicon.size}"
href={env.PUBLIC_SERVER_URL + favicon.favicon.url} />
Expand Down
13 changes: 13 additions & 0 deletions src/routes/favicon.ico/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { env } from "$env/dynamic/public";

export async function GET() {
const res = await (
await fetch(env.PUBLIC_SERVER_URL + `/api/globals/main-info`)
).json();
const file = await (
await fetch(env.PUBLIC_SERVER_URL + res.favicon.url)
).blob();
const response = new Response(file);
response.headers.set("Content-Type", "image/x-icon");
return response;
}
10 changes: 0 additions & 10 deletions src/routes/news/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { setLayout, tertiaryLayout, tertiaryLayoutDark } from "$lib/setLayout";
import { env } from "$env/dynamic/public";
import BlockRenderer from "$lib/BlockRenderer.svelte";
import { seoInfo } from "$lib/seoInfo";
import { onMount } from "svelte";
import DarkModeIcon from "$lib/DarkModeIcon.svelte";
import LightModeIcon from "$lib/LightModeIcon.svelte";
Expand All @@ -13,15 +12,6 @@

export let data;

seoInfo.update(seoInfo => {
seoInfo.title = data.News.title
seoInfo.description = data.News.description
seoInfo.type = "article"
seoInfo.publishDate = data.News.publishDate

return seoInfo
})

let mode = "light"
let buttonColor
let textColor
Expand Down