Skip to content

Commit

Permalink
Feat: allow SEO to be turned on off or conditonally set
Browse files Browse the repository at this point in the history
  • Loading branch information
MotorTruck1221 committed Dec 21, 2024
1 parent 8ff35b3 commit 967bc5a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
5 changes: 5 additions & 0 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export default defineConfig({
access: 'public',
default: parsedDoc.buildOpts.games,
}),
SEO: envField.boolean({
context: 'client',
access: 'public',
default: Boolean(Deno.env.get('SEO')) || parsedDoc.seo.enabled
})
},
},
vite: {
Expand Down
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dev:server": "deno run -A --watch server/server.ts",
"dev:client": "astro dev",
"dev": "deno task dev:server & deno task dev:client",
"build:seo": "deno task check:frontend && SEO=true astro build --outDir dist/seo/ && SEO=false astro build --outDir dist/noseo/",
"build": "deno task check:frontend && astro build",
"start": "deno run --allow-net --allow-read --allow-env --allow-ffi --allow-sys server/server.ts",
"bstart": "deno task build && deno task start",
Expand Down
21 changes: 20 additions & 1 deletion server/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ interface Data {
wisp: boolean;
port: number;
};
//other options can be added later.
seo: {
enabled: boolean;
both: boolean;
domain: string;
};
}

const doc = await Deno.readTextFile(`${Deno.cwd()}/config.toml`);
Expand All @@ -29,5 +33,20 @@ if (typeof parsedDoc.server.wisp !== "boolean") {
if (typeof parsedDoc.server.port !== "number") {
throw new Error(`Invalid type for "server.port"! It should be a number`);
}
if (typeof parsedDoc.seo.enabled !== "boolean") {
throw new Error(`Invalid type for "seo.enabled"! It should be an boolean (true/false)`);
}
if (typeof parsedDoc.seo.both !== "boolean") {
throw new Error(`Invalid type for "seo.both"! It should be an boolean (true/false)`);
}
if (typeof parsedDoc.seo.domain !== "string") {
throw new Error(`Invalid type for "seo.domain"! It should be an string`);
} else {
try {
new URL(parsedDoc.seo.domain);
} catch (e: any) {
throw new Error(e);
}
}

export { type Data as TOMLConfig, parsedDoc };
16 changes: 14 additions & 2 deletions server/standalone/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@ import { serveStatic } from 'jsr:@hono/hono/deno';
import { compress } from 'jsr:@hono/hono/compress';
import { listeningMessage } from '../message.ts';
import { parsedDoc } from '../config/config.ts';

const app = new Hono();

app.use(compress({
encoding: 'gzip',
}));

app.use('/*', serveStatic({ root: `${Deno.cwd()}/dist` }));
if (parsedDoc.seo.enabled && !parsedDoc.seo.both || !parsedDoc.seo.enabled) {
app.use('/*', serveStatic({ root: `${Deno.cwd()}/dist` }));
}

if (parsedDoc.seo.enabled && parsedDoc.seo.both) {
app.use('/*', (ctx, next) => {
if (new URL(ctx.req.url).host === new URL(parsedDoc.seo.domain).host) {
return serveStatic({ root: `${Deno.cwd()}/dist/seo` })(ctx, next);
}
else {
return serveStatic({ root: `${Deno.cwd()}/dist/noseo` })(ctx, next);
}
});
}

Deno.serve({
hostname: '0.0.0.0',
Expand Down
29 changes: 15 additions & 14 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import { SEO } from 'astro-seo';
import { SEO as SEOConf } from "astro:env/client";
interface Props {
title?: string;
Expand All @@ -20,33 +21,33 @@ import SettingsLoader from '@components/settings/Loader.astro';
<LoadScripts transition:persist />
<SEO
title={title}
titleTemplate="Incognito | %s"
titleDefault="Incognito"
description="Search the world wide web"
titleTemplate=`${SEOConf ? "Incognito | %s" : "%s"}`
titleDefault=`${SEOConf ? 'Incognito': ''}`
description=`${SEOConf ? 'Search the world wide web': ''}`
charset="UTF-8"
openGraph = {{
basic: {
title: "Incognito",
title: SEOConf ? "Incognito": '',
type: "website",
url: "https://incog.works",
image: "/logo.svg"
url: SEOConf ? "https://incog.works": '',
image: SEOConf ? "/logo.svg": ''
},
optional: {
description: "Search the world wide web"
description: SEOConf ? "Search the world wide web": ''
}
}}
twitter = {{
card: "summary_large_image",
url: "https://incog.works",
title: "Incognito",
description: "Search the world wide web",
image: "/logo.svg",
imageAlt: "Incognito's logo"
url: SEOConf ? "https://incog.works": '',
title: SEOConf ? "Incognito": '',
description: SEOConf ? "Search the world wide web": '',
image: SEOConf ? "/logo.svg": '',
imageAlt: SEOConf ? "Incognito's logo": ''
}}
extend = {{
link: [
{ rel: "icon", type: "image/svg+xml", href: "/logo.svg", id: "favicon" },
{ rel: "sitemap", href: "/sitemap-index.xml" },
SEOConf && { rel: "sitemap", href: "/sitemap-index.xml" },
{ rel: "preconnect", href: "https://fonts.googleapis.com" },
{ rel: "preconnect", href: "https://fonts.gstatic.com", crossorigin: true },
{ rel: "stylesheet", href: "https://fonts.googleapis.com/css2?family=Ubuntu+Mono&display=swap" },
Expand All @@ -55,7 +56,7 @@ import SettingsLoader from '@components/settings/Loader.astro';
],
meta: [
{ name: "viewport", content: "width=device-width, initial-scale=1.0" },
{ name: "title", content: "Incognito" },
{ name: "title", content: SEOConf ? "Incognito": '' },
]
}}
/>
Expand Down

0 comments on commit 967bc5a

Please sign in to comment.