Skip to content

Commit

Permalink
feature/restore products page with Lit v3 (#12)
Browse files Browse the repository at this point in the history
* restore products page with Lit v3

* patch missing SSR page chunks

* patch in namespacing of SSR page chunks

* narrow chunks on chunk naming convention

* upgrade latest greenwood alpha.1

* remove patch package
  • Loading branch information
thescientist13 authored Mar 16, 2024
1 parent d34d6b0 commit 16cbfd4
Show file tree
Hide file tree
Showing 9 changed files with 816 additions and 744 deletions.
1,489 changes: 773 additions & 716 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"keywords": [
"Greenwood",
"Vercel",
"WCC",
"Web Components",
"Serverless",
"Edge",
Expand All @@ -24,16 +23,16 @@
"clean": "rimraf public .greenwood .vercel",
"dev": "greenwood develop",
"build": "greenwood build",
"serve": "greenwood build && greenwood serve",
"serve": "npm run clean && greenwood build && greenwood serve",
"start": "npm run serve"
},
"dependencies": {
"lit": "^2.8.0"
"lit": "^3.1.0"
},
"devDependencies": {
"@greenwood/cli": "~0.29.0",
"@greenwood/plugin-adapter-vercel": "~0.29.0",
"@greenwood/plugin-renderer-lit": "~0.29.0",
"@greenwood/cli": "~0.30.0-alpha.1",
"@greenwood/plugin-adapter-vercel": "~0.30.0-alpha.1",
"@greenwood/plugin-renderer-lit": "~0.30.0-alpha.1",
"rimraf": "^5.0.0"
}
}
8 changes: 5 additions & 3 deletions src/api/fragment.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { render } from '@lit-labs/ssr/lib/render-with-global-dom-shim.js';
import { render } from '@lit-labs/ssr';
import { collectResult } from '@lit-labs/ssr/lib/render-result.js'
import { html } from 'lit';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import { getProducts } from '../services/products.js';
import { renderFromHTML } from '../services/render-to-string.js';
import '../components/card.js';

export const isolation = true;

export async function handler(request) {
const params = new URLSearchParams(request.url.slice(request.url.indexOf('?')));
const limit = params.has('limit') ? parseInt(params.get('limit'), 10) : 5;
const offset = params.has('offset') ? parseInt(params.get('offset'), 10) : 0;
const products = (await getProducts()).slice(offset, offset + limit);
const body = await renderFromHTML(render(html`
const body = await collectResult(render(html`
${
unsafeHTML(products.map((item, idx) => {
const { title, thumbnail } = item;
Expand Down
8 changes: 5 additions & 3 deletions src/api/search.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { render } from '@lit-labs/ssr/lib/render-with-global-dom-shim.js';
import { render } from '@lit-labs/ssr';
import { collectResult } from '@lit-labs/ssr/lib/render-result.js'
import { html } from 'lit';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import { getProducts } from '../services/products.js';
import { renderFromHTML } from '../services/render-to-string.js';
import '../components/card.js';

export const isolation = true;

export async function handler(request) {
const formData = await request.formData();
const term = formData.has('term') ? formData.get('term') : '';
Expand All @@ -17,7 +19,7 @@ export async function handler(request) {
if (products.length === 0) {
body = 'No results found.';
} else {
body = await renderFromHTML(render(html`
body = await collectResult(render(html`
${
unsafeHTML(products.map((item, idx) => {
const { title, thumbnail } = item;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h2>JSON API</h2>
<h2 id="greeting-output"></h2>
</article>
<article>
<h2>Fragments API (w/ WCC)</h2>
<h2>Fragments API (w/ Lit+SSR)</h2>
<p>This is an example of a Greenwood API route returning an HTML response that is generated by server-rendering a Web Component (with Declarative Shadow DOM). This same component is loaded on the client-side too, so that when you click the <i>Product Details</i> button, state and interactivity can still be resumed. You can see it fire in the network tab as <i>/api/fragment</i></p>
<div id="load-products-output" class="products-cards-container" aria-live="polite"></div>
<button id="load-products">Load More Products</button>
Expand Down
23 changes: 23 additions & 0 deletions src/pages/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { html } from 'lit';
import { getProducts } from '../services/products.js';
import '../components/card.js';

export const isolation = true;

export async function getBody() {
const products = await getProducts();

return html`
${
products.map((product, idx) => {
const { title, thumbnail } = product;
return html`
<app-card
title="${idx + 1}) ${title}"
thumbnail="${thumbnail}"
></app-card>
`;
})
}
`;
}
2 changes: 1 addition & 1 deletion src/pages/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<body>
<article>
<h2>Search API (w/ WCC)</h2>
<h2>Search API (w/ Lit+SSR)</h2>
<p>This is an example of a Greenwood API leveraging the FormData API and returning an HTML response that is generated by server-rendering a Web Component (with Declarative Shadow DOM). This same component is loaded on the client-side too, so that when you click the <i>Item Details</i> button, state and interactivity can still be resumed. You can see it fire in the network tab as <i>/api/search</i></p>
<form id="search">
<label for="term">
Expand Down
13 changes: 0 additions & 13 deletions src/services/render-to-string.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/templates/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ <h1>Demos</h1>
<a href="https://github.com/ProjectEvergreen/greenwood-demo-adapter-vercel" title="Link to demonstration repo">Greenwood demonstration repo</a>
showcasing various features deployed to and running on Vercel serverless functions.
</p>
<page-outlet></page-outlet>
<section class="products-cards-container">
<page-outlet></page-outlet>
</section>
</main>

<footer>
Expand Down

0 comments on commit 16cbfd4

Please sign in to comment.