Skip to content

Commit

Permalink
Fix building static sites with Astro DB (#10655)
Browse files Browse the repository at this point in the history
* Fix building static sites with Astro DB

* Adding a changeset

* Try a different port range
  • Loading branch information
matthewp authored Apr 2, 2024
1 parent a79e6b1 commit b1eda3d
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-elephants-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Pass through appToken on static sites with Astro DB
1 change: 1 addition & 0 deletions packages/db/src/core/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export async function cmd({
tables: dbConfig.tables ?? {},
appToken: appToken.token,
isBuild: false,
output: 'server',
});
} else {
virtualModContents = getLocalVirtualModContents({
Expand Down
2 changes: 2 additions & 0 deletions packages/db/src/core/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function astroDBIntegration(): AstroIntegration {
tables,
root: config.root,
srcDir: config.srcDir,
output: config.output,
});
} else {
dbPlugin = vitePluginDb({
Expand All @@ -67,6 +68,7 @@ function astroDBIntegration(): AstroIntegration {
seedFiles,
root: config.root,
srcDir: config.srcDir,
output: config.output,
});
}

Expand Down
15 changes: 13 additions & 2 deletions packages/db/src/core/integration/vite-plugin-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SEED_DEV_FILE_NAME } from '../../runtime/queries.js';
import { DB_PATH, RUNTIME_CONFIG_IMPORT, RUNTIME_IMPORT, VIRTUAL_MODULE_ID } from '../consts.js';
import type { DBTables } from '../types.js';
import { type VitePlugin, getDbDirectoryUrl, getRemoteDatabaseUrl } from '../utils.js';
import type { AstroConfig } from 'astro';

const WITH_SEED_VIRTUAL_MODULE_ID = 'astro:db:seed';

Expand All @@ -26,13 +27,15 @@ type VitePluginDBParams =
seedFiles: LateSeedFiles;
srcDir: URL;
root: URL;
output: AstroConfig['output'];
}
| {
connectToStudio: true;
tables: LateTables;
appToken: string;
srcDir: URL;
root: URL;
output: AstroConfig['output'];
};

export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
Expand Down Expand Up @@ -66,6 +69,7 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
appToken: params.appToken,
tables: params.tables.get(),
isBuild: command === 'build',
output: params.output,
});
}
return getLocalVirtualModContents({
Expand Down Expand Up @@ -141,15 +145,22 @@ export function getStudioVirtualModContents({
tables,
appToken,
isBuild,
output,
}: {
tables: DBTables;
appToken: string;
isBuild: boolean;
output: AstroConfig['output'];
}) {
function appTokenArg() {
if (isBuild) {
// In production build, always read the runtime environment variable.
return 'process.env.ASTRO_STUDIO_APP_TOKEN';
if(output === 'server') {
// In production build, always read the runtime environment variable.
return 'process.env.ASTRO_STUDIO_APP_TOKEN';
} else {
// Static mode or prerendering needs the local app token.
return `process.env.ASTRO_STUDIO_APP_TOKEN ?? ${JSON.stringify(appToken)}`;
}
} else {
return JSON.stringify(appToken);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/db/test/fixtures/static-remote/astro.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import astroDb from '@astrojs/db';
import { defineConfig } from 'astro/config';

export default defineConfig({
integrations: [astroDb()],
});
12 changes: 12 additions & 0 deletions packages/db/test/fixtures/static-remote/db/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { column, defineDb, defineTable } from 'astro:db';

const User = defineTable({
columns: {
id: column.number({ primaryKey: true }),
name: column.text(),
},
});

export default defineDb({
tables: { User },
});
9 changes: 9 additions & 0 deletions packages/db/test/fixtures/static-remote/db/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { User, db } from 'astro:db';

export default async function () {
await db.insert(User).values([
{
name: 'Houston'
}
]);
}
16 changes: 16 additions & 0 deletions packages/db/test/fixtures/static-remote/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@test/db-static-remote",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@astrojs/db": "workspace:*",
"astro": "workspace:*"
}
}
20 changes: 20 additions & 0 deletions packages/db/test/fixtures/static-remote/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
import { User, db } from 'astro:db';
const users = await db.select().from(User);
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>

<h2>Users</h2>
<ul>
{users.map(user => (
<li>{user.name}</li>
))}
</ul>
</body>
</html>
34 changes: 34 additions & 0 deletions packages/db/test/static-remote.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from '../../astro/test/test-utils.js';
import { setupRemoteDbServer } from './test-utils.js';

describe('astro:db', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/static-remote/', import.meta.url),
output: 'static',
});
});

describe('static build --remote', () => {
let remoteDbServer;

before(async () => {
remoteDbServer = await setupRemoteDbServer(fixture.config);
await fixture.build();
});

after(async () => {
await remoteDbServer?.stop();
});

it('Can render page', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerioLoad(html);

expect($('li').length).to.equal(1);
});
});
});
2 changes: 1 addition & 1 deletion packages/db/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const singleQuerySchema = z.object({

const querySchema = singleQuerySchema.or(z.array(singleQuerySchema));

let portIncrementer = 8081;
let portIncrementer = 8030;

/**
* @param {import('astro').AstroConfig} astroConfig
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b1eda3d

Please sign in to comment.