投稿時間:2023-01-22 22:12:27 RSSフィード2023-01-22 22:00 分まとめ(16件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
AWS lambdaタグが付けられた新着投稿 - Qiita AWS Serverless デジタルバッジを取得する② Lambda編 https://qiita.com/oishi-d/items/69993a14b566af8cdb75 awsserverless 2023-01-22 21:43:46
python Pythonタグが付けられた新着投稿 - Qiita Pythonでmultiprocessing(並列処理)を簡単に実装+可視化してくれるライブラリを作ってみた https://qiita.com/Domzou/items/03d39dd1cdce46719c94 multiprocessing 2023-01-22 21:23:01
js JavaScriptタグが付けられた新着投稿 - Qiita 【JS】非同期処理とは https://qiita.com/myu-kata/items/e4ea269b38bb66393452 ajaxasynchronousjava 2023-01-22 21:38:15
AWS AWSタグが付けられた新着投稿 - Qiita AWS Serverless デジタルバッジを取得する② Lambda編 https://qiita.com/oishi-d/items/69993a14b566af8cdb75 awsserverless 2023-01-22 21:43:46
AWS AWSタグが付けられた新着投稿 - Qiita 【AWS】AWS Batch(Fargate タイプ)の CLI コマンドメモ https://qiita.com/oiz-y/items/6662354651093c7fa456 awsbatchfargate 2023-01-22 21:22:41
Git Gitタグが付けられた新着投稿 - Qiita GitHub Flowとは https://qiita.com/YuNaga224/items/66585a66958707887447 feature 2023-01-22 21:12:56
海外TECH DEV Community Nx Module Federation bad Angular routing https://dev.to/this-is-angular/nx-module-federation-bad-angular-routing-1ac9 Nx Module Federation bad Angular routingWhile puzzling around with Nx Webpack Module Federation support I stumbled upon an issue that strangely looks like none cared about before I m talking about duplication of remote component in presence of a lt router outlet gt in its template when served as independent app Its poor traction could be due to the fact the bug shows up only using an Angular Standalone Components setup at least for remotes The issueI will take for granted the creation of a Module Federated Angular app under Nx After scaffolding we should have an host app acting as shell of our multi module project and at least one remote app Both of them are Micro Frontends meaning they are capable of running on their own even the remote one That comes clear issuing nx serve host project name where they will be served independently giving you the chance to look at their behaviour both when remote is accessed as child route of the host both when it s booted as standalone app To make thing barely functional our host app consists of a single dumb component with two lines template Component selector testapp root template lt a routerLink remote app gt Remote lt a gt lt router outlet gt lt router outlet gt export class RemoteEntryComponent Just an anchor linking to the remote and a router outlet under which the child will be rendered Similarly the remote will be just a visual aid to show our issue Component selector testapp remote entry template lt div style background color blue height px width px margin px gt lt div gt export class RemoteEntryComponent Here we got merely a square div coloured in blue That s it Let s look at what gets served then For our host app we ll get just the link that once clicked will route to the remote that will appear right under it and under host s lt router outlet gt If we browse to the standalone remote served app by default reachable at host port thus on http localhost for a classic setup we ll see our remote content the blue square immediately rendered and no host s anchor element as expected So where s the problem The issue we re talking about arises if we add a lt router outlet gt element to our remote This is quite a common situation many times our microfrontend will have its own internal routing with child paths and obviously a router outlet under which rendering them This is our modified remote Component selector testapp remote entry template lt div style background color blue height px width px margin px gt lt div gt lt router outlet gt export class RemoteEntryComponent In this short GIF we can see how it still works flawlessy when rendered as child of our host app while acting really strangely when browsing to its standalone serving Our remote is rendered twice The reasonTo understand the reason behind this wrong behaviour we got to dive a little deep into tech used by Nx plugins to manage Webpack Module Federation for Angular apps For the host app there s nothing too fancy it gets served as standalone and if needed is able to load external modules through a little twist of normal routing path remote app loadChildren gt loadRemoteModule rem Routes then m gt m remoteRoutes See how as callback of Angular Router s loadChildren instead of classic import path to lazy loaded module we got a call to Nx provided loadRemoteModule remote module name exposed routes path Inside our remote s module federation config js there s the mapping for arguments passed to that call module exports name rem exposes Routes apps rem src app remote entry entry routes ts So the host s Router will look inside remote s entry route ts to know which child routes it could render This is default generated entry route tsexport const remoteRoutes Route path component RemoteEntryComponent As we could imagine there s just a root path pointing to default remote s root component commonly referred as entry component This is nice for a situation where our remote is treated as accessory module of a main app All its structure is just a lazy loaded branch inside host routing tree Things drastically change when our remote has to be served as an independent app Without a parent app already up and running it needs to be bootstrapped as any other Angular application The flow Nx relies on starts with the usual serving of an index html built upon a template like this lt DOCTYPE html gt lt html lang en gt lt head gt lt head gt lt body gt lt testapp remote entry gt lt testapp remote entry gt lt body gt lt html gt In place of Angular cli generated lt app root gt lt app root gt selector assigned to AppComponent body has been populated with our custom remote entry component s selector The code then sources main ts as always that delegates to bootstrap ts file Here takes place the actual application bootstrapping that for an Angular Standalone Components setup concept unrelated to standalone serving of our remote don t get confused could be enough something like bootstrapApplication RemoteEntryComponent Nx remotes generator takes one step further providing to our Router eventual inner routes defined for this remote import appRoutes from app app routes bootstrapApplication RemoteEntryComponent providers importProvidersFrom RouterModule forRoot appRoutes initialNavigation enabledBlocking This app app routes simply lazy loads the same entry routes file exposed to our host app export const appRoutes Route path loadChildren gt import remote entry entry routes then m gt m remoteRoutes And here comes the problem We ve already seen that file provides a route to root path rendering the root component of our remote But in our remote independent serving that component is already rendered due to its reference inside remote s index html template This graph should be explanatory enough As the purple highlighted labels emphasize the path to rendering of the entry component for independent remote serving encounters two different locations asking for it That entry component is actually both declared both routed Why the issue doesn t occur with old NgModule based remotes setup instead of standalone components Because Nx remotes generator for classic ng modular setup doesn t need to declare the entry component inside index html It declares a separate root component AppComponent hosting a lt router outlet gt and imports RouterModule inside root module AppModule This way the same route referring to entry component will be correctly rendered just once both for Shell setup than for independent remote serving My solutionThe problem can be probably solved in many ways since every sane solution involves a partial rewriting of Nx generators for remotes built upon Angular standalone components Mi idea is to distinguish the purpose of remote s app routes and entry routes At the moment the former simply lazyloads the second leading to an identical initial routing flow for both serving contexts Instead I thought of turning app routes into the real route provider for remote and leaving entry routes just as a plugin invoked only in Shell serving context keeping needed root path pointing to entry component and importing app routes to use defined routes as its children To do this first we modify app routes removing its entry routes lazy load and adding our remote children routes to its array To keep entry routes inside our compilation unit we move its import outside of routes definition so it will be still compiled and exposed by our federation configuration but ignored by our router during independent serving commented out original default entry routes import export const appRoutes Route path loadChildren gt import remote entry entry routes then m gt m remoteRoutes import remote entry entry routes then m gt m remoteRoutes export const appRoutes Route path first child route component FirstChildRouteComponent path second child route component SecondChildRouteComponent path third child route component ThirdChildRouteComponent Now we can edit entry routes enriching its only route with an array of children simply importing the one defined in app routesimport appRoutes from app routes import RemoteEntryComponent from entry component export const remoteRoutes Route path component RemoteEntryComponent children appRoutes In the end we can appreciate the result of our efforts looking at our blue square rendered a single time for any serving methodology ConclusionsI think Nx support for Angular standalone components federation is still a bit unripe I found a couple oddities in its implementation I ll spend some words about in a future article maybe It does a great job though raising the coder from writing a lot of boilerplate and considering I m not exactly an expert in this field there s the chance some of the things looking like flaws to me were real solutions for cornercases or common use scenarios I didn t think of For the very same reason the one I presented as a solution could be sub optimal if not even wrong for other situations I didn t test it extensively that s why as usual you re highly encouraged to leave a comment if anything doesn t sound right Cheers 2023-01-22 12:37:33
ニュース @日本経済新聞 電子版 ANA客室乗務員、週2日勤務可能に 副業や地方居住促進 https://t.co/ZyysjWgtqx https://twitter.com/nikkei/statuses/1617145110650568704 客室乗務員 2023-01-22 13:00:14
ニュース @日本経済新聞 電子版 EV保守整備、損保ジャパンが参入 関連事業の裾野広がる https://t.co/EDraoq88UI https://twitter.com/nikkei/statuses/1617141713474326529 損保ジャパン 2023-01-22 12:46:44
ニュース @日本経済新聞 電子版 ポケモン・スプラトゥーン3が大ヒット 任天堂、もろ刃の自前主義 https://t.co/jaQfKkFNHP https://t.co/OhIdDrdPXX https://twitter.com/nikkei/statuses/1617137550568689665 自前主義 2023-01-22 12:30:11
ニュース @日本経済新聞 電子版 岸田首相、黒田日銀総裁の交代明言 2月に人事案提示 https://t.co/9GOxfHaZQv https://twitter.com/nikkei/statuses/1617132156488396800 日銀総裁 2023-01-22 12:08:45
ニュース @日本経済新聞 電子版 Web3振興、規制・税制の柔軟な見直しを https://t.co/2R2rBDOgd3 https://twitter.com/nikkei/statuses/1617132155494371329 規制 2023-01-22 12:08:45
ニュース @日本経済新聞 電子版 米国LA郊外で銃撃9人死亡 アジア系住民多い地区 https://t.co/TadOhou52l https://twitter.com/nikkei/statuses/1617130362416816130 銃撃 2023-01-22 12:01:37
ニュース BBC News - Home Call for probe into claims BBC chair helped Johnson secure loan https://www.bbc.co.uk/news/uk-politics-64362640?at_medium=RSS&at_campaign=KARANGA johnson 2023-01-22 12:29:12
ニュース BBC News - Home Crackdown on energy firms over rise in prepayment meters https://www.bbc.co.uk/news/business-64364195?at_medium=RSS&at_campaign=KARANGA customers 2023-01-22 12:43:18
仮想通貨 BITPRESS(ビットプレス) [日経] Web3振興、規制・税制の柔軟な見直しを 坂井豊貴・慶応義塾大学教授 https://bitpress.jp/count2/3_9_13531 慶応義塾大学 2023-01-22 21:50:46

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)