Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce a render function for SSR #33

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions examples/lit-hydrate/component.ts

This file was deleted.

28 changes: 0 additions & 28 deletions examples/lit-hydrate/example.ts

This file was deleted.

34 changes: 34 additions & 0 deletions examples/lit-ssr/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import UAParser from 'ua-parser-js'
import { LitElement, css, html } from 'lit';

export class SimpleGreeting extends LitElement {
static shadowRootOptions: ShadowRootInit = { mode: 'closed' }

static properties = {
name: {},
};

// Define scoped styles right with your component, in plain CSS
static styles = css`
:host {
color: blue;
}
`;

name: string;

constructor() {
super();
const ua = new UAParser(navigator.userAgent);

// Declare reactive properties
this.name = ua.getBrowser().name || 'World';
}

// Render the UI as a function of component state
render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}

customElements.define('simple-greeting', SimpleGreeting);
48 changes: 48 additions & 0 deletions examples/lit-ssr/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import path from 'node:path'
import Koa from 'koa'

import { run, render, type RunnerArgs, createSession } from '../../dist/index.js'

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const app = new Koa()

app.use(async (ctx) => {
const browserName = ctx.query.browserName || 'chrome'
const browserVersion = ctx.query.browserVersion || 'canary'
const sessionName = ctx.query.sessionName
const runParams: RunnerArgs = { browserName, browserVersion, rootDir: __dirname }

if (sessionName) {
runParams.sessionName = sessionName
}

if (ctx.path === '/favicon.ico') {
return
}

/**
* Example of server side rendering without using an SSR helper
*/
if (ctx.path === '/render') {
ctx.body = await render(/*html*/`
<script type="module" src="/component.ts"></script>
<simple-greeting></simple-greeting>
`, runParams)
return
}

/**
* Example of server side rendering using an SSR helper
*/
ctx.body = await run(async () => {
const { render } = await import('@lit-labs/ssr');
const { html } = await import('lit');
await import('./component.ts');

const dom = await render(html`<simple-greeting></simple-greeting>`);
return Array.from(dom).join('\n')
}, runParams)
})

app.listen(3000)
console.log('Server running at http://localhost:3000/');

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"type": "module",
"dependencies": {
"koa": "^2.15.0",
"lit": "^3.1.2"
"lit": "^3.1.2",
"ua-parser-js": "^1.0.37"
},
"devDependencies": {
"@lit-labs/ssr": "^3.2.2"
"@lit-labs/ssr": "^3.2.2",
"@types/ua-parser-js": "^0.7.39"
}
}
Loading