refactor(api-key): Inject Ghost API key at build time

This commit is contained in:
2026-02-17 19:21:42 +01:00
parent 9a3b35b428
commit 99ddd47649
3 changed files with 9 additions and 10 deletions

View File

@@ -16,9 +16,11 @@ jobs:
uses: actions/setup-node@v4 uses: actions/setup-node@v4
with: with:
node-version: '20' node-version: '20'
cache: 'pnpm'
- name: Install dependencies - name: Install dependencies
run: pnpm install run: pnpm install
- name: Create Ghost Config
run: |
echo "window.GhostConfig = { ghostApiKey: '${{ secrets.GHOST_CONTENT_API_KEY }}', ghostApiUrl: '${{ secrets.GHOST_ADMIN_API_URL }}' };" > assets/js/ghost-config.js
- name: Build theme - name: Build theme
run: pnpm build run: pnpm build
- name: Deploy Ghost Theme - name: Deploy Ghost Theme

View File

@@ -102,16 +102,14 @@ document.addEventListener('DOMContentLoaded', () => {
let isLoading = false; let isLoading = false;
let hasMorePosts = true; let hasMorePosts = true;
const ghostApiKeyMeta = document.querySelector('meta[name="ghost-api-key"]'); // Retrieve API key and URL from global GhostConfig object
const ghostApiUrlMeta = document.querySelector('meta[name="ghost-api-url"]'); if (typeof window.GhostConfig === 'undefined' || !window.GhostConfig.ghostApiKey || !window.GhostConfig.ghostApiUrl) {
console.error('Ghost Content API Key or URL not found in window.GhostConfig. Infinite scroll will not work.');
if (!ghostApiKeyMeta || !ghostApiUrlMeta) {
console.error('Ghost Content API Key or URL meta tag not found. Infinite scroll will not work.');
return; return;
} }
const GHOST_API_KEY = ghostApiKeyMeta.content; const GHOST_API_KEY = window.GhostConfig.ghostApiKey;
const GHOST_API_URL = ghostApiUrlMeta.content; const GHOST_API_URL = window.GhostConfig.ghostApiUrl;
const fetchPosts = async () => { const fetchPosts = async () => {
if (isLoading || !hasMorePosts) return; if (isLoading || !hasMorePosts) return;

View File

@@ -6,8 +6,6 @@
<title>{{meta_title}}</title> <title>{{meta_title}}</title>
<link rel="stylesheet" href="{{asset "built/screen.css"}}"> <link rel="stylesheet" href="{{asset "built/screen.css"}}">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&family=Playfair+Display:wght@700&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&family=Playfair+Display:wght@700&display=swap" rel="stylesheet">
<meta name="ghost-api-key" content="YOUR_GHOST_CONTENT_API_KEY"> <!-- IMPORTANT: Replace with your actual Ghost Content API Key -->
<meta name="ghost-api-url" content="{{@site.url}}">
{{ghost_head}} {{ghost_head}}
</head> </head>
<body class="{{body_class}} font-sans antialiased"> <body class="{{body_class}} font-sans antialiased">
@@ -30,6 +28,7 @@
</div> </div>
{{ghost_foot}} {{ghost_foot}}
<script src="{{asset "js/ghost-config.js"}}"></script>
<script src="{{asset "js/main.js"}}"></script> <script src="{{asset "js/main.js"}}"></script>
</body> </body>
</html> </html>