Skip to content

Commit

Permalink
fix: ssr issues
Browse files Browse the repository at this point in the history
  • Loading branch information
htmujahid committed May 9, 2024
1 parent 6ce5f0f commit 93f55aa
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 77 deletions.
3 changes: 1 addition & 2 deletions apps/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"vite": "^5.2.6"
},
"dependencies": {
"@sonnetjs/core": "0.0.23",
"@sonnetjs/html": "0.0.1"
"@sonnetjs/core": "0.0.23"
}
}
44 changes: 12 additions & 32 deletions apps/playground/src/Counter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { $component, SonnetComponent } from '@sonnetjs/core';
import { a, button, div, h1, img, p } from '@sonnetjs/html';

class Counter extends SonnetComponent {
counter = 0;
Expand All @@ -15,37 +14,18 @@ class Counter extends SonnetComponent {
}

public get() {
return div()
.children(
a()
.href('https://vitejs.dev')
.target('blank')
.children(
img()
.src('https://vitejs.dev/logo.svg')
.className('logo')
.alt('Vite Logo')
.get(),
)
.get(),
h1().innerText('Vite').get(),
div()
.className('card')
.children(
button()
.id('counter')
.type('button')
.innerText(`count is ${this.counter}`)
.className('btn')
.get(),
)
.get(),
p()
.innerText('Edit src/main.ts and save to test HMR.')
.className('read-the-docs')
.get(),
)
.get();
return /*html*/ `
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="https://vitejs.dev/logo.svg" class="logo" alt="Vite Logo" />
</a>
<h1>Vite</h1>
<div class="card">
<button id="counter" type="button" class="btn">count is ${this.counter}</button>
</div>
<p class="read-the-docs">Edit src/main.ts and save to test HMR.</p>
</div>
`;
}
}

Expand Down
3 changes: 1 addition & 2 deletions apps/ssr-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"compression": "^1.7.4",
"express": "^4.18.2",
"sirv": "^2.0.4",
"@sonnetjs/core": "*",
"@sonnetjs/html": "0.0.2"
"@sonnetjs/core": "*"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand Down
44 changes: 12 additions & 32 deletions apps/ssr-playground/src/Counter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { $component, SonnetComponent } from '@sonnetjs/core';
import { a, button, div, h1, img, p } from '@sonnetjs/html';

class Counter extends SonnetComponent {
counter = 0;
Expand All @@ -15,37 +14,18 @@ class Counter extends SonnetComponent {
}

public get() {
return div()
.children(
a()
.href('https://vitejs.dev')
.target('blank')
.children(
img()
.src('https://vitejs.dev/logo.svg')
.className('logo')
.alt('Vite Logo')
.get(),
)
.get(),
h1().innerText('Vite').get(),
div()
.className('card')
.children(
button()
.id('counter')
.type('button')
.innerText(`count is ${this.counter}`)
.className('btn')
.get(),
)
.get(),
p()
.innerText('Edit src/main.ts and save to test HMR.')
.className('read-the-docs')
.get(),
)
.get();
return /*html*/ `
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="https://vitejs.dev/logo.svg" class="logo" alt="Vite Logo" />
</a>
<h1>Vite</h1>
<div class="card">
<button id="counter" type="button" class="btn">count is ${this.counter}</button>
</div>
<p class="read-the-docs">Edit src/main.ts and save to test HMR.</p>
</div>
`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/ssr-playground/src/entry-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { createApp } from '@sonnetjs/core';
import Counter from './Counter';

const app = createApp();
app.root(Counter());
app.root(Counter);
app.ssr();
app.mount('#app');
2 changes: 1 addition & 1 deletion packages/sonnet-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sonnetjs/core",
"version": "0.0.23",
"version": "0.0.24",
"files": [
"dist"
],
Expand Down
14 changes: 8 additions & 6 deletions packages/sonnet-core/src/core/SonnetApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ export class SonnetApp {
this._mountedId = selector;
return;
}
if (!this._ssr && isBrowser()) {
if (isBrowser()) {
const el = document.querySelector(selector);
if (el && this._component) {
const component = this.initRootComponent();

const getComponent = await component?.get();
if (!this._ssr) {
const getComponent = await component?.get();

if (typeof getComponent === 'string') {
el.innerHTML = getComponent as string;
} else if (getComponent instanceof Element) {
el.appendChild(getComponent);
if (typeof getComponent === 'string') {
el.innerHTML = getComponent as string;
} else if (getComponent instanceof Element) {
el.appendChild(getComponent);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tooling/tailwind/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"extends": "@repo/typescript-config/base.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
}
}

0 comments on commit 93f55aa

Please sign in to comment.