Skip to content

Commit

Permalink
feat(vercel): streaming (#8879)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy authored Nov 7, 2023
1 parent 35739d0 commit 754e4fd
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/tricky-clocks-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astrojs/vercel': minor
---

The Vercel adapter now streams responses!

This brings better performance to your visitors by showing them content as it is rendered. The browser can also start loading the required stylesheets and scripts much sooner, which ultimately results in faster full page loads.
3 changes: 2 additions & 1 deletion packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ interface CreateFunctionFolderArgs {
NTF_CACHE: any;
includeFiles: URL[];
excludeFiles?: string[];
maxDuration?: number;
maxDuration: number | undefined;
}

async function createFunctionFolder({
Expand Down Expand Up @@ -381,6 +381,7 @@ async function createFunctionFolder({
handler,
launcherType: 'Nodejs',
maxDuration,
supportsResponseStreaming: true,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';

export default defineConfig({
output: "server",
adapter: vercel()
});
10 changes: 10 additions & 0 deletions packages/integrations/vercel/test/fixtures/streaming/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@test/vercel-streaming",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "workspace:*"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>One</title>
</head>
<body>
<h1>One</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Two</title>
</head>
<body>
<h1>Two</h1>
</body>
</html>
15 changes: 8 additions & 7 deletions packages/integrations/vercel/test/static-assets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ describe('Static Assets', () => {

const VALID_CACHE_CONTROL = 'public, max-age=31536000, immutable';

async function build({ adapter, assets }) {
async function build({ adapter, assets, output }) {
fixture = await loadFixture({
root: './fixtures/static-assets/',
output,
adapter,
build: {
assets,
Expand Down Expand Up @@ -38,31 +39,31 @@ describe('Static Assets', () => {
}

describe('static adapter', async () => {
const adapter = await import('@astrojs/vercel/static');
const { default: vercel } = await import('@astrojs/vercel/static');

it('has cache control', async () => {
await build({ adapter });
await build({ adapter: vercel() });
checkValidCacheControl();
});

it('has cache control other assets', async () => {
const assets = '_foo';
await build({ adapter, assets });
await build({ adapter: vercel(), assets });
checkValidCacheControl(assets);
});
});

describe('serverless adapter', async () => {
const adapter = await import('@astrojs/vercel/serverless');
const { default: vercel } = await import('@astrojs/vercel/serverless');

it('has cache control', async () => {
await build({ adapter });
await build({ output: "server", adapter: vercel() });
checkValidCacheControl();
});

it('has cache control other assets', async () => {
const assets = '_foo';
await build({ adapter, assets });
await build({ output: "server", adapter: vercel(), assets });
checkValidCacheControl(assets);
});
});
Expand Down
21 changes: 21 additions & 0 deletions packages/integrations/vercel/test/streaming.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';

describe('maxDuration', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/streaming/',
});
await fixture.build();
});

it('makes it to vercel function configuration', async () => {
const vcConfig = JSON.parse(
await fixture.readFile('../.vercel/output/functions/render.func/.vc-config.json')
);
expect(vcConfig).to.deep.include({ supportsResponseStreaming: true });
});
});
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 754e4fd

Please sign in to comment.