diff --git a/astro.config.ts b/astro.config.ts index 8049581e4..a32b70318 100644 --- a/astro.config.ts +++ b/astro.config.ts @@ -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: { diff --git a/deno.jsonc b/deno.jsonc index 9a567712f..fc1292260 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -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", diff --git a/server/config/config.ts b/server/config/config.ts index 3940b974a..3bd6c153d 100644 --- a/server/config/config.ts +++ b/server/config/config.ts @@ -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`); @@ -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 }; diff --git a/server/standalone/standalone.ts b/server/standalone/standalone.ts index d0dbd16a4..d8edbc484 100644 --- a/server/standalone/standalone.ts +++ b/server/standalone/standalone.ts @@ -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', diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index a1ff22b5d..96351cd00 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -1,5 +1,6 @@ --- import { SEO } from 'astro-seo'; +import { SEO as SEOConf } from "astro:env/client"; interface Props { title?: string; @@ -20,33 +21,33 @@ import SettingsLoader from '@components/settings/Loader.astro';