Skip to content

Commit

Permalink
fix: ssr hyderation
Browse files Browse the repository at this point in the history
  • Loading branch information
htmujahid committed Oct 2, 2024
1 parent c97877f commit 2dca28c
Show file tree
Hide file tree
Showing 19 changed files with 55 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion packages/create-sonnet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-sonnet-app",
"version": "0.0.17",
"version": "0.0.18",
"description": "",
"main": "index.js",
"type": "module",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"vite": "^5.2.6"
},
"dependencies": {
"@sonnetjs/core": "0.0.32"
"@sonnetjs/core": "0.0.33"
}
}
2 changes: 1 addition & 1 deletion packages/create-sonnet/template/code/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"vite": "^5.2.6"
},
"dependencies": {
"@sonnetjs/core": "0.0.32"
"@sonnetjs/core": "0.0.33"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"vite": "^5.2.6"
},
"dependencies": {
"@sonnetjs/core": "0.0.32",
"@sonnetjs/core": "0.0.33",
"@sonnetjs/router": "0.0.5"
}
}
2 changes: 1 addition & 1 deletion packages/create-sonnet/template/code/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"vite": "^5.2.6"
},
"dependencies": {
"@sonnetjs/core": "0.0.32",
"@sonnetjs/core": "0.0.33",
"@sonnetjs/router": "0.0.5"
}
}
2 changes: 1 addition & 1 deletion packages/create-sonnet/template/code/ssr-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"compression": "^1.7.4",
"express": "^4.18.2",
"sirv": "^2.0.4",
"@sonnetjs/core": "0.0.32"
"@sonnetjs/core": "0.0.33"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand Down
4 changes: 0 additions & 4 deletions packages/create-sonnet/template/code/ssr-ts/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ app.use('*', async (req, res) => {
}
});

app.get('/', (req, res) => {
res.redirect(301, '/home');
});

// Start http server
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
2 changes: 1 addition & 1 deletion packages/create-sonnet/template/code/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"compression": "^1.7.4",
"express": "^4.18.2",
"sirv": "^2.0.4",
"@sonnetjs/core": "0.0.32"
"@sonnetjs/core": "0.0.33"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand Down
4 changes: 0 additions & 4 deletions packages/create-sonnet/template/code/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ app.use('*', async (req, res) => {
}
});

app.get('/', (req, res) => {
res.redirect(301, '/home');
});

// Start http server
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
Expand Down
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.32",
"version": "0.0.33",
"files": [
"dist"
],
Expand Down
4 changes: 2 additions & 2 deletions packages/sonnet-core/src/core/SonnetApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ export class SonnetApp {
if (el && this._component) {
const component = this.initRootComponent();

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

if (!this._ssr) {
if (typeof getComponent === 'string') {
el.innerHTML = getComponent as string;
} else if (getComponent instanceof Element) {
Expand Down
32 changes: 17 additions & 15 deletions packages/sonnet-core/src/core/factory.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { SonnetHead } from '@sonnetjs/shared';
import { isBrowser, SonnetHead } from '@sonnetjs/shared';

import { EventEmitter } from './Event';
import SonnetComponent from '../abstract/SonnetComponent';

const event = EventEmitter.getInstance();

interface Component<T> {
new (args?: T): SonnetComponent;
new(args?: T): SonnetComponent;
head?: () => void;
script?: () => void;
}

export function $component<T>(component: Component<T>) {
return (args?: T) => {
const instance = new component(args);
// head tags
if (component.head && component.head.toString() !== 'head(){return""}') {
event.once<SonnetHead>('head', component.head as () => SonnetHead);
}
if (instance.head && instance.head.toString() !== 'head(){return""}') {
event.on<SonnetHead>('head', instance.head.bind(instance));
}
// scripts
if (component.script && component.script.toString() !== 'script(){}') {
event.once('script', component.script);
}
if (instance.script && instance.script.toString() !== 'script(){}') {
event.on('script', instance.script.bind(instance));
if (isBrowser()) {
// head tags
if (component.head && component.head.toString() !== 'head(){return""}') {
event.once<SonnetHead>('head', component.head as () => SonnetHead);
}
if (instance.head && instance.head.toString() !== 'head(){return""}') {
event.on<SonnetHead>('head', instance.head.bind(instance));
}
// scripts
if (component.script && component.script.toString() !== 'script(){}') {
event.once('script', component.script);
}
if (instance.script && instance.script.toString() !== 'script(){}') {
event.on('script', instance.script.bind(instance));
}
}

return instance as SonnetComponent;
Expand Down
2 changes: 1 addition & 1 deletion packages/sonnet-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"typescript": "^5.3.3"
},
"peerDependencies": {
"@sonnetjs/core": "0.0.32"
"@sonnetjs/core": "0.0.33"
},
"dependencies": {
"path-to-regexp": "^8.2.0"
Expand Down
6 changes: 1 addition & 5 deletions playground/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express from 'express';

// Constants
const isProduction = process.env.NODE_ENV === 'production';
const port = process.env.PORT || 5174;
const port = process.env.PORT || 3000;
const base = process.env.BASE || '/';

// Cached production assets
Expand Down Expand Up @@ -65,10 +65,6 @@ app.use('*', async (req, res) => {
}
});

app.get('/', (req, res) => {
res.redirect(301, '/home');
});

// Start http server
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
Expand Down
18 changes: 18 additions & 0 deletions playground/ssr/src/App.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { $component, SonnetComponent } from '@sonnetjs/core';
import Counter from './Counter';

class App extends SonnetComponent {
public script() {
console.log('script');
}

get() {
return /*html*/ `
<div>
${Counter().get()}
</div>
`;
}
}

export default $component(App);
7 changes: 4 additions & 3 deletions playground/ssr/src/entry-client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import './style.css';
import { createApp } from '@sonnetjs/core';
import Counter from './Counter';

import App from './App';

const app = createApp();
app.root(Counter);
app.root(App);
app.ssr();
app.mount('#app');
app.mount('#app');
4 changes: 2 additions & 2 deletions playground/ssr/src/entry-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Counter from './Counter';
import App from './App';

export async function render() {
const html = await Counter().get();
const html = await App().get();
return { html };
}

0 comments on commit 2dca28c

Please sign in to comment.