python |
Pythonタグが付けられた新着投稿 - Qiita |
「2022 慶応義塾大学 総合政策学部【5】」をsympyとFreeCADでやってみたい。 |
https://qiita.com/mrrclb48z/items/b2cb3f952c789952611d
|
youtube |
2023-08-19 17:59:29 |
python |
Pythonタグが付けられた新着投稿 - Qiita |
Prompt Flowでプロンプトを定量的に評価する |
https://qiita.com/sakue_103/items/41d14e96a253a820bc0f
|
excel |
2023-08-19 17:44:01 |
Linux |
Ubuntuタグが付けられた新着投稿 - Qiita |
【小ネタ】sarコマンド入力後に出る「Cannot open /var/log/sysstat/saXX: No such file or directory Please check if data collecting is enabled」の解決策 |
https://qiita.com/vallack5296/items/d6729f2447462a29f2f5
|
【小ネタ】sarコマンド入力後に出る「CannotopenvarlogsysstatsaXXNosuchfileordirectoryPleasecheckifdatacollectingisenabled」の解決策事象sysstatパッケージは既にインストール済みで、実行環境はUbuntuです。 |
2023-08-19 17:19:15 |
AWS |
AWSタグが付けられた新着投稿 - Qiita |
【備忘録】AWSのセキュリティ対策について学んだことまとめ ~インフラ編② セッションマネージャーからプライベートサブネットに接続~ |
https://qiita.com/Yukimaru_Rutla/items/3094ece353677815a59c
|
配置 |
2023-08-19 17:00:40 |
Azure |
Azureタグが付けられた新着投稿 - Qiita |
Prompt Flowでプロンプトを定量的に評価する |
https://qiita.com/sakue_103/items/41d14e96a253a820bc0f
|
excel |
2023-08-19 17:44:01 |
海外TECH |
MakeUseOf |
5 Sites to Understand Cognitive Biases and Make Better Decisions |
https://www.makeuseof.com/sites-to-understand-cognitive-biases-make-better-decisions/
|
biases |
2023-08-19 08:30:21 |
海外TECH |
DEV Community |
Service Worker Templating Language (SWTL) |
https://dev.to/thepassle/service-worker-templating-language-swtl-47e5
|
Service Worker Templating Language SWTL I ve previously written about Service Worker Side Rendering SWSR in this blog when I was exploring running Astro in a Service Worker I recently had a usecase for a small app at work and I just kind of defaulted to a SPA At some point I realized I needed a Service Worker for my app and I figured why not have the entire app rendered by the Service Worker All I need to do was fetch some data some light interactivity that I don t need a library or framework for and stitch some html partials together based on that data If I did that in a Service Worker I could stream in the html as well While I was able to achieve this fairly easily the developer experience of manually stitching strings together wasnt great Being myself a fan of buildless libraries such as htm and lit html I figured I d try to take a stab at implementing a DSL for component like templating in Service Workers myself called Service Worker Templating Language SWTL here s what it looks like import html Router from swtl import BreadCrumbs from BreadCrumbs js function HtmlPage children title return html lt html gt lt head gt lt title gt title lt title gt lt head gt lt body gt children lt body gt lt html gt function Footer return html lt footer gt Copyright lt footer gt const router new Router routes path render params query request gt html lt HtmlPage title Home gt lt h gt Home lt h gt lt nav gt lt BreadCrumbs path request url pathname gt lt nav gt fetch some partial html caches match another partial html lt ul gt foo bar baz map i gt html lt li gt i lt li gt lt ul gt lt Footer gt lt gt self addEventListener fetch event gt if event request mode navigate event respondWith router handleRequest event request htmlTo create this DSL I m using Tagged Template Literals For those of you who are not familiar with them here s what they look like function html statics dynamics console log statics console log dynamics html hello world hello world A Tagged Template Literal gets passed an array of static values string and an array of dynamic values expressions Based on those strings and expressions I can parse the result and add support for reusable components I figured that since I m doing this in a Service Worker I m only creating html responses and not doing any kind of diffing I should be able to just return a stitched together array of values and components Based on preact htm s component syntax I built something like this function Foo return html lt h gt foo lt h gt const world world const template html lt h gt Hello world lt h gt lt Foo gt lt h gt Hello world lt h gt fn Foo children properties I can then create a render function to serialize the results and stream the html to the browser render is also a generator function that takes care of stringifying values and actually calling the component functions so their html gets rendered too const iterator render html hello world const encoder new TextEncoder const stream new ReadableStream async pull controller const value done await iterator next if done controller close else controller enqueue encoder encode value Will stream the response to the browser as results are coming in from our iterable new Response stream However I then realized that since I m streaming the html anyways instead of waiting for a template to be parsed entirely and return an array why not stream the templates as they are being parsed Consider the following example function html statics dynamics for let i i lt statics length i yield statics i if dynamics i yield dynamics i Using a generator function we can yield results as we encounter them and stream those results to the browser immediately We can then iterate over the template results const template html hello world for const chunk of template console log chunk hello world What makes this even cooler is that we can provide first class support for other streamable things like iterables function generator yield html lt li gt lt li gt yield html lt li gt lt li gt html lt ul gt generator lt ul gt Or other streams or Responses html fetch some html html caches match some html html Why not do this at build time The following template const template html lt h gt hi lt h gt lt Foo prop gt bar lt gt Would compile to something like const template lt h gt hi lt h gt fn Foo properties name prop value children bar While admittedly that would save a little runtime overhead it would increase the bundlesize of the service worker itself Considering the fact that templates are streamed while they are being parsed I m not convinced pre compiling templates would actually result in a noticeable difference Also I m a big fan of buildless development and libraries like lit html and preact htm and the bundlesize for the html function itself is small enough Isomorphic renderingWhile I m using this library in a Service Worker only you can also use this library for isomorphic rendering in worker like environments or even just on any node like JS runtime and the browser The following code will work in any kind of environment function Foo return html lt h gt hi lt h gt const template html lt main gt lt Foo gt lt main gt const result await renderToString template lt main gt lt h gt hi lt h gt lt main gt Hurray for agnostic libraries RouterI also implemented a simple router based on URLPattern so you can easily configure your apps routes import Router html from swtl const router new Router routes path render gt html lt HtmlPage gt lt h gt Home lt h gt lt gt path users id render params gt html lt HtmlPage gt lt h gt User params id lt h gt lt gt path foo render params query request gt html lt HtmlPage gt lt h gt request url pathname lt h gt lt gt self addEventListener fetch event gt if event request mode navigate event respondWith router handleRequest event request Out of order streamingI also wanted to try and take a stab at out of order streaming for cases where you may need to fetch some data While you could do something like this async function SomeComponent try const data await fetch api foo then r gt r json return html lt ul gt data map user gt html lt li gt user name lt li gt lt ul gt catch return html Failed to fetch data This would make the api call blocking and stop streaming html until the api call resolves and we can t really show a loading state Instead we ship a special lt Async gt component that takes an asynchronous task function to enable out of order streaming import Async when html from swtl html lt Async task gt fetch api foo then r gt r json gt state data error gt html lt h gt Fetching data lt h gt when state pending gt html lt Spinner gt when state error gt html Failed to fetch data when state success gt html lt ul gt data map user gt html lt li gt user name lt li gt lt ul gt lt gt When an Async component is encountered it kicks off the task that is provided to it and immediately stream render the pending state and continues streaming the rest of the document When the rest of the document is has finished streaming to the browser we await all the promises in order of resolution the promise that resolves first gets handled first and replace the pending result with either the error or success template based on the result of the task So considering the following code html lt HtmlPage gt lt h gt home lt h gt lt ul gt lt li gt lt Async task gt new Promise r gt setTimeout gt r foo foo gt state data gt html when state pending gt html PENDING slow when state error gt html ERROR slow when state success gt html RESOLVED slow lt gt lt li gt lt li gt lt Async task gt new Promise r gt setTimeout gt r bar bar gt state data gt html when state pending gt html PENDING fast when state error gt html ERROR fast when state success gt html RESOLVED fast lt gt lt li gt lt ul gt lt h gt footer lt h gt lt gt This is the result We can see that the entire document is streamed initially displaying loading states Then once the promises resolve the content is updated in place to display the success state Wrapping upI ve created an initial version of swtl to NPM and so far it seems to hold up pretty well for my app but please let me know if you run into any bugs or issues Lets make it better together Acknowledgementslit htmlpreact htmAstro and Matthew Philips For doing the hard work of implementing the rendering logic back when I requested this in astroArtem Zakharchenko For helping with the handling of first resolve first serve promisesAlvar Lagerlöf For a cool demo of out of order streaming which largely influenced my implementationAnd it s also good to mention that while working tweeting about some of the work in progress updates of this project it seems like many other people had similar ideas and even similar implementations as well It s always cool to see different people converging on the same idea |
2023-08-19 08:27:56 |
ニュース |
BBC News - Home |
Storm Betty: Disruption due to flooding and fallen trees |
https://www.bbc.co.uk/news/uk-northern-ireland-66553551?at_medium=RSS&at_campaign=KARANGA
|
extra |
2023-08-19 08:57:05 |
ニュース |
BBC News - Home |
I worked on the Teenage Mutant Ninja Turtles movie from thousands of miles away |
https://www.bbc.co.uk/news/uk-scotland-edinburgh-east-fife-66533036?at_medium=RSS&at_campaign=KARANGA
|
edinburgh |
2023-08-19 08:26:19 |
ニュース |
BBC News - Home |
We are building an Iron Age village by hand |
https://www.bbc.co.uk/news/uk-scotland-66478550?at_medium=RSS&at_campaign=KARANGA
|
village |
2023-08-19 08:25:56 |
ニュース |
BBC News - Home |
Made In Heaven: A show taking on all that's wrong with Indian weddings |
https://www.bbc.co.uk/news/world-asia-india-66541837?at_medium=RSS&at_campaign=KARANGA
|
marriages |
2023-08-19 08:09:28 |
ニュース |
BBC News - Home |
Michael Johnson column: Zharnel Hughes has world medal chance in wide-open sprints |
https://www.bbc.co.uk/sport/athletics/66548873?at_medium=RSS&at_campaign=KARANGA
|
Michael Johnson column Zharnel Hughes has world medal chance in wide open sprintsIn his BBC Sport column Michael Johnson discusses Zharnel Hughes chances in the wide open sprint events at the World Championships |
2023-08-19 08:24:09 |
コメント
コメントを投稿