Skip to content

Commit

Permalink
make sure shared bundles across API routes and SSR pages get included…
Browse files Browse the repository at this point in the history
… in adapter upload bundles (#1149)
  • Loading branch information
thescientist13 authored Sep 1, 2023
1 parent 28f0ed4 commit a7b18b3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
11 changes: 11 additions & 0 deletions packages/plugin-adapter-netlify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ async function netlifyAdapter(compilation) {
);
}

const ssrApiAssets = (await fs.readdir(new URL('./api/', outputDir)))
.filter(file => new RegExp(/^[\w][\w-]*\.[a-zA-Z0-9]{4,20}\.[\w]{2,4}$/).test(path.basename(file)));

for (const asset of ssrApiAssets) {
await fs.cp(
new URL(`./${asset}`, new URL('./api/', outputDir)),
new URL(`./${asset}`, outputRoot),
{ recursive: true }
);
}

// NOTE: All functions must live at the top level
// https://github.com/netlify/netlify-lambda/issues/90#issuecomment-486047201
await createOutputZip(id, outputType, outputRoot, projectDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* api/
* fragment.js
* greeting.js
* submit.js
* search.js
* submit-form-data.js
* submit-json.js
* components/
* card.js
* pages/
Expand Down Expand Up @@ -76,11 +78,11 @@ describe('Build Greenwood With: ', function() {
});

it('should output the expected number of serverless function zip files', function() {
expect(zipFiles.length).to.be.equal(6);
expect(zipFiles.length).to.be.equal(7);
});

it('should output the expected number of serverless function API zip files', function() {
expect(zipFiles.filter(file => path.basename(file).startsWith('api-')).length).to.be.equal(4);
expect(zipFiles.filter(file => path.basename(file).startsWith('api-')).length).to.be.equal(5);
});

it('should output the expected number of serverless function SSR page zip files', function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { renderFromHTML } from 'wc-compiler';
import { getArtists } from '../services/artists.js';

export async function handler(request) {
const formData = await request.formData();
const term = formData.has('term') ? formData.get('term') : '';
const artists = (await getArtists())
.filter((artist) => {
return term !== '' && artist.name.toLowerCase().includes(term.toLowerCase());
});
let body = '';

if (artists.length === 0) {
body = 'No results found.';
} else {
const { html } = await renderFromHTML(`
${
artists.map((item, idx) => {
const { name, imageUrl } = item;
return `
<app-card
title="${idx + 1}) ${name}"
thumbnail="${imageUrl}"
></app-card>
`;
}).join('')
}
`, [
new URL('../components/card.js', import.meta.url)
]);

body = html;
}

return new Response(body, {
headers: new Headers({
'Content-Type': 'text/html'
})
});
}
11 changes: 11 additions & 0 deletions packages/plugin-adapter-vercel/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ async function vercelAdapter(compilation) {
new URL('./assets/', outputRoot),
{ recursive: true }
);

const ssrApiAssets = (await fs.readdir(new URL('./api/', outputDir)))
.filter(file => new RegExp(/^[\w][\w-]*\.[a-zA-Z0-9]{4,20}\.[\w]{2,4}$/).test(path.basename(file)));

for (const asset of ssrApiAssets) {
await fs.cp(
new URL(`./${asset}`, new URL('./api/', outputDir)),
new URL(`./${asset}`, outputRoot),
{ recursive: true }
);
}
}

// static assets / build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* api/
* fragment.js
* greeting.js
* submit.js
* search.js
* submit-form-data.js
* submit-json.js
* components/
* card.js
* pages/
Expand Down Expand Up @@ -77,7 +79,7 @@ describe('Build Greenwood With: ', function() {
});

it('should output the expected number of serverless function output folders', function() {
expect(functionFolders.length).to.be.equal(6);
expect(functionFolders.length).to.be.equal(7);
});

it('should output the expected configuration file for the build output', function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { renderFromHTML } from 'wc-compiler';
import { getArtists } from '../services/artists.js';

export async function handler(request) {
const formData = await request.formData();
const term = formData.has('term') ? formData.get('term') : '';
const artists = (await getArtists())
.filter((artist) => {
return term !== '' && artist.name.toLowerCase().includes(term.toLowerCase());
});
let body = '';

if (artists.length === 0) {
body = 'No results found.';
} else {
const { html } = await renderFromHTML(`
${
artists.map((item, idx) => {
const { name, imageUrl } = item;
return `
<app-card
title="${idx + 1}) ${name}"
thumbnail="${imageUrl}"
></app-card>
`;
}).join('')
}
`, [
new URL('../components/card.js', import.meta.url)
]);

body = html;
}

return new Response(body, {
headers: new Headers({
'Content-Type': 'text/html'
})
});
}

0 comments on commit a7b18b3

Please sign in to comment.