-
Notifications
You must be signed in to change notification settings - Fork 28
[SSR]: Add server side rendering support for Nala components #943
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
Open
fallaciousreasoning
wants to merge
2
commits into
main
Choose a base branch
from
ssr-wc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "ssr", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@types/express": "^5.0.0", | ||
"express": "^4.21.2", | ||
"tsx": "^4.19.2", | ||
"typescript": "^5.7.2" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<meta | ||
name="description" | ||
content="Web site created using HTML & Web Components" | ||
/> | ||
<link href="variables.css" rel="stylesheet" /> | ||
<title>Web Components</title> | ||
<style> | ||
body { | ||
background: var(--leo-color-page-background); | ||
color: var(--leo-color-text-primary); | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', | ||
'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', | ||
'Helvetica Neue', sans-serif; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<leo-button>Click Me</leo-button> | ||
<leo-button href="https://brave.com" kind="outline" | ||
>External link</leo-button | ||
> | ||
<leo-input placeholder="Input"> Custom Label for Input </leo-input> | ||
<leo-checkbox checked>Checkbox</leo-checkbox> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import express from 'express' | ||
import { render } from '../../src/ssr/render' | ||
import fs from 'fs/promises' | ||
|
||
const app = express() | ||
|
||
app.get('/icons/:name', async (req, res) => { | ||
const icon = await fs.readFile(`../../icons/${req.params.name}`, 'utf-8') | ||
res.type('svg') | ||
res.send(icon) | ||
}) | ||
|
||
app.get('/variables.css', async (req, res) => { | ||
const css = await fs.readFile('../../tokens/css/variables.css', 'utf-8') | ||
res.type('css') | ||
res.send(css) | ||
}) | ||
|
||
app.get('/', async (req, res) => { | ||
const rawHtml = await fs.readFile('./raw.html', 'utf-8') | ||
|
||
const html = await render(rawHtml, [ | ||
() => import('../../web-components/button'), | ||
() => import('../../web-components/checkbox'), | ||
() => import('../../web-components/input') | ||
]) | ||
res.type('html') | ||
res.send(html) | ||
}) | ||
|
||
app.listen(3000, () => { | ||
console.log('Server is running on port 3000') | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, | ||
"module": "commonjs" /* Specify what module code is generated. */, | ||
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, | ||
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, | ||
"strict": true /* Enable all strict type-checking options. */, | ||
"skipDefaultLibCheck": true | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Window } from 'happy-dom' | ||
|
||
const globals = new Window() | ||
|
||
global.window = globals.window as any | ||
global.document = globals.document as any | ||
global.customElements = globals.customElements as any | ||
global.HTMLElement = globals.HTMLElement as any | ||
global.MutationObserver = globals.MutationObserver as any | ||
|
||
/** | ||
* Renders custom elements to the DOM, so they render before JavaScript is | ||
* loaded. Elements which require JavaScript for interactivity will **NOT** | ||
* work until the JavaScript loads. | ||
* @param html The HTML to render | ||
* @param imports The custom elements to load - these must be imported | ||
* dynamically so HappyDOM can load before they are imported. | ||
* @returns The rendered HTML | ||
*/ | ||
export async function render( | ||
html: string, | ||
imports: (() => Promise<unknown>)[] = [] | ||
) { | ||
await Promise.all(imports.map((init) => init())) | ||
|
||
const document = globals.document | ||
document.documentElement.innerHTML = html | ||
thypon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await globals.happyDOM.waitUntilComplete() | ||
|
||
return document.documentElement.getHTML({ serializableShadowRoots: true }) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.