Execute server-only code inside site.ts #99
-
Hello, first of all - thank you for your awesome project 👍 Only thing i've haven't found yet is if there's a way you can run server-only functions like Here's an example down below (using child_process to get latest GIT hash): import { execSync } from "child_process";
const commitHash = execSync('git rev-parse --short HEAD').toString().trim();
export default {
title: 'îles',
description: 'Islands of interactivity with Vue in Vite.js',
commitHash
} Thanks in advance! 😄 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @marcopixel! You can run server-only code behind a let commitHash
if (import.meta.env.SSR) {
const { execSync } = require('child_process')
commitHash = execSync('git rev-parse --short HEAD').toString().trim()
} However, if you want a more "universal" approach, I would recommend using an env var with export default defineConfig({
vite: {
define: {
'import.meta.env.APP_GIT_SHA': JSON.stringify(commitHash),
},
},
}) Just in case, here's a related example, but setting git timestamps to the frontmatter of each page. |
Beta Was this translation helpful? Give feedback.
Hi @marcopixel!
You can run server-only code behind a
import.meta.env.SSR
guard. For example:However, if you want a more "universal" approach, I would recommend using an env var with
define
, for example iniles.config.ts
where you can use node directly:Just in case, here's a related example, but setting git timestamps to the frontmatter of each page.