beenotung / ts-liveview

Build hybrid SSG and SSR realtime SPA/MPA with Typescript
https://liveviews.cc
BSD 2-Clause "Simplified" License
167 stars 2 forks source link

demo: on each page link to setup template #6

Open hpvd opened 2 years ago

hpvd commented 2 years ago

would be perfectly helpful to understand how it's done asap, to have on every page of the demo a direct deeplink to it's tsx template file

e.g. on: https://liveviews.cc/auto-complete link to: https://github.com/beenotung/ts-liveview/blob/master/server/app/pages/auto-complete-demo.tsx

beenotung commented 2 years ago

Good suggestion, I'll add a link to the source file of each pages, maybe also show the code with syntax highlight

hpvd commented 2 years ago

maybe also show the code with syntax highlight

yeah! As always there is a tool advertising... The lightest suitable tool I know so far is prismjs https://github.com/PrismJS/prism/

beenotung commented 2 years ago

prismjs looks like great choice, mush smaller than highlight.js

beenotung commented 2 years ago

I can get the basic syntax highlight working; trying to figure out how to enable two languages syntax highlight at the same time.

With class="lang-tsx": 2

With class="lang-css": 1

hpvd commented 2 years ago

great!

regarding second language: the example code is a scroll box (hard to see) where highlighting of inline code is demonstrated: https://prismjs.com/#examples

got there from this issue and the mentioned merge: https://github.com/PrismJS/prism/issues/2776

beenotung commented 2 years ago

Updated the deployed version to show source code of each demo pages.

Will explore using regex to extract /* css */ and /* html */ and wrap them into corresponding <code class="language-xxx"> (hence there will be nested <code>)

Screenshot of current version https://liveviews.cc/auto-complete: 1

beenotung commented 2 years ago

Maybe it would be helpful to omit the import statements for simplicity.

(Updated to hide import statements by default but able to show them after click to expand (using <details> and <summary>)

hpvd commented 2 years ago

looks great in the demo!

hpvd commented 2 years ago

just found, the prism.js file seems to be loaded twice (found it because seeing a flickering when loading the page with throttled speed)

btw: the file also looks a little"huge" 11/31KB. Would have expected a little less, even with all languages included (min+gzipped).

2022-06-05_20h29_16

beenotung commented 2 years ago

The double loading phenomenon is expected, because the style and script tag is included in the component, not directly in the app skeleton, hence if the component is used multiple times, the resources will be loaded multiple times. On another hand, if the component is not used in that page, it's not loaded.

Normally, subsequent loading should be answered from the browser cache.

I can think up a workaround, to inject the resources only once by marking a flag in the render context, and inject them in a place of dom that will not be removed throughout the whole life cycle.

This workaround may work but it'll (slightly) increase the complexity.

hpvd commented 2 years ago

when starting to dive a little deeper into the stunning demo by looking into webdev tools, on the very first look the direct loading of prism.js and prism.css is a little diluting/diminishing the great benefits of your solution: size, speed and simplicity.

Of course it's great to be able to directly look into the setup and one should keep it!

Since you already hide the setup code and make users to click a button to show it -> what do you think about loading all the resources needed for showing the setup on this click too?

beenotung commented 2 years ago

what do you think about loading all the resources needed for showing the setup on this click too?

That is a great suggestion!

We can load the additional js and css upon the click event.

In additional, the long code that are hidden by default can also be loaded lazily.

That is possible for clients with javascript enabled, but it'll require a full page load for clients without/disabled javascript. So I prefer to keep the code content inline but load the js and css lazily.

On the other hand, I'll try to do the parsing and code generation (wrapping span with classes) on the server at boot-time or build-time. This way may result in lighter runtime 🤔