Back to Question Center
0

Yin Magana tare da Asynchronous APIs a cikin Sake Sake Sake Gida            Yin Magana tare da Asynchronous APIs a cikin Sakon Server-Sake Shafin Farko: ES6Raw Semalt

1 answers:
Tattaunawa tare da Asynchronous APIs a cikin Server-Sake Gida

Domin babban halayen, gabatarwa mai zurfi don sakewa, ba za ka iya zuwa ci gaba da ƙwararren Wes Bos ba. Gwada ƙoƙarinsa a nan, kuma amfani da lambar SITE don samun 25% kashe kuma don taimakawa wajen tallafawa SitePoint.

Idan ka taba yin amfani da shafin yanar gizo, za a iya sha wahala daga matalauta SEO da kuma yin abubuwan da ke faruwa akan na'urori masu hankali - coyote slot machine. Kuna iya ƙara saitunan gargajiya na al'ada na shafukan intanet, musamman tare da NodeJS, amma wannan ba hanya mai sauƙi ba ne, musamman ma APIs asynchronous.

Abubuwa biyu masu mahimmanci da ka samu daga yin fasalin lambobinka akan uwar garke sune:

  • ƙara karuwa a lokacin lokatai
  • inganta sassaucin SEO.

Ka tuna cewa Google yana jira don Semalt ya ɗauka, abubuwa masu sauki irin su abun ciki suna canza ba tare da fitowar ba. (Ba zan iya yin magana akan wasu injunan binciken ba, ko da yake, ko yadda abin dogara ne.)

A cikin wannan post, Zan tattauna samun bayanai daga asynchronous APIs lokacin amfani da uwar garke-sanya React code. Dokar da take da shi ta ƙunshi dukan tsarin da aka gina a cikin JavaScript. Wannan yana nufin cewa, ba kamar misalai na MVC na al'ada tare da mai kulawa ba, baku san abin da kuke buƙatar ba har sai an aika da app. Tare da tsarin kamar Ƙirƙirar Abubuwa, zaku iya ƙirƙirar aikace-aikacen aiki mai mahimmanci, amma yana buƙatar ku rike da ma'anar kawai a kan abokin ciniki. Akwai abun da ke gudana tare da wannan, da kuma batun ƙaddamarwa, inda kayan injunan gargajiya na al'ada za ka iya canza kansa kamar yadda ka ga ya dace.

Matsala

Siffofin tsaftace-tsaren daidaitawa don yawancin bangare, don haka idan ba ku da bayanai, kuna sa allon loading da jira don bayanan da za su zo. Wannan ba ya aiki sosai daga uwar garken, saboda ba ku san abin da kuke buƙata ba har sai kun yi, ko ku san abin da kuke buƙatar amma kuna riga kuka fassara.

Ya ƙaddamar da wannan tsarin hanyar daidaitaccen abu:

     Sahihanci. sa (           , takardun. GetElementById ('tushen'))    

Sakamakon:

  1. Yana da DOM ya nema neman ainihin tushen. Wannan ba ya kasance a kan uwar garke, saboda haka dole mu raba wannan.
  2. Ba mu da damar shiga wani abu a waje da tushen mu. Ba za mu iya saita lambobin Facebook ba, suna, bayanin, daban-daban SEO tags, kuma ba mu da iko a kan sauran DOM a waje da kashi, musamman shugaban.
  3. Muna samar da wasu jihohin, amma uwar garke da abokin ciniki suna da jihohin daban. Muna buƙatar la'akari da yadda za a rike wannan jiha (a wannan yanayin, Redux).

Saboda haka Semalt ya yi amfani da ɗakunan karatu guda biyu a nan, kuma suna da kyau sosai, saboda haka yana fatan zai kai ga sauran ɗakunan karatu da kake yin amfani da su.

Redux : Ajiye yanayin inda uwargijinka da abokinka suka haɗa shi shine batun mafarki mai ban tsoro. Yana da tsada sosai, kuma yawancin yakan haifar da kwari. A gefen uwar garke, ƙila, ba ka so ka yi wani abu tare da Redux ba tare da isasshen isa don samun kayan aiki da yin daidai daidai ba. (Zaku iya amfani da shi a matsayin al'ada, kawai ya sanya isasshen jihar don yin kama da abokin ciniki.) Idan kuna son gwadawa, duba hanyoyin daban-daban na tsarin rarraba matsayin farawa.

Maimaitawa : FYI, wannan shi ne v4 version, wanda shine abin da aka shigar da tsoho, amma yana da mahimmanci idan kun sami aikin tsofaffi. Kana buƙatar tabbatar da cewa kayi rike da gefen uwar garken sakonka da gefen abokin ciniki tare da v4 - kuma yana da kyau a wannan.

Bayan haka, me idan kuna buƙatar yin kira na cibiyar sadarwa? Nan da nan wannan ya zama babban batu, saboda yana da async kuma yana cikin bangarenku.

Dole kuyi domin sanin abin da kuke buƙata - wanda ya kamata a ƙayyade a lokacin jinkirin - kuma don ɗaukar waɗannan ƙidodi kafin ku bauta wa abokin ku.

Matakan da ke ciki

A ƙasa, Gudun hankali yana duba maganganun da suke a yanzu don bayar da shawarar warware matsalar.

Gaba. js

Kafin mu tafi ko ina, idan kana son samar da kayan aiki, hanyar sabuntawa ta hanyar sabuntawa ko aikace-aikacen duniya, Semalt] shine inda kake so ka tafi. Yana aiki, yana da tsabta, kuma yana da goyon bayan Zeit.

Tsakanin, yana da ra'ayi, dole ne ka yi amfani da kayan aiki na kayan aiki, kuma yadda suke amfani da async data loading ba dole bane.

Bincika wannan kwafin takardun daga takardun Semalt repo:

     shigo da amsa daga 'amsa'fitarwa ajiyar ajiyar ƙara ƙara. Abun {async samo asali naInitialProps ({req}) {sake dawowa? {userAgent: req. masu sanya kawunansu ['mai amfani-wakili']}: {userAgent: navigator. mai amfaniAgent}}sa    {dawo  
Sannu Duniya {wannan. props. mai amfaniAgent}
}}

GetInitialProps shine maɓallin kewayawa a can, wanda ya sake dawo da alkawarin da ya kawo ga wani abu da ke nuna goyon baya, kuma kawai a shafi. Abin da ke da kyau shi ne kawai aka gina a cikin kayan aiki: ƙara da shi aiki, babu aiki da ake bukata!

To, yaya zaka samu bayanan bayanai? Kuna kira API. Ba ku so? To, wannan yayi kyau. (Na'am, don haka zaka iya ƙara abubuwa da ke al'ada, amma dole ne ka aiwatar da shi da kanka.) Idan kayi tunanin wannan, ko da yake, yana da kyau sosai, kuma, a kullum magana, aikin kirki, domin in ba haka ba, abokinka zai ci gaba da yin wannan API kira, kuma latency a kan uwar garke ne kusan negligible.

Har ila yau, ƙayyadadden ƙayyadadden abin da ke da damar samun dama ga - kyawawan abu kawai ne kawai; Har ila yau, wannan alama kamar aikin kirki, saboda ba ku da damar yin amfani da ku, wanda zai bambanta a kan uwar garke da abokin ciniki ko ta yaya. Oh, kuma idan ba ku kama shi ba, to kawai yana aiki ne a kan matakan da ke saman shafin.

Redux Connect

Redux Connect wani ƙwararren sakonni ne mai ƙwarewa, tare da kyakkyawan falsafar, amma idan ba ku yi amfani da duk kayan aikin da suka bayyana ba, wannan ba zai kasance ba a gare ku. Yana da yawa ga wannan kunshin, amma yana da matukar hadari kuma ba'a kyautatawa zuwa React Router v4 ba. Yayi yawaita saiti ga wannan, amma bari mu dauki bangare mafi muhimmanci, kawai don koyi wasu darussa:

     // 1. Haɗa bayananku, kama da amsa-redux @connect@asyncConnect ({{key: 'abincin rana',alkawalin: ({params, helpers}) => Wa'adin. warware ({id: 1, sunan: 'Borsch'})}])Kwamfuta na App ya ƙara ƙara. Abun {sa    {// 2. samo bayanai don taimakawaconst lunch = wannan. props. abincin ranadawo ( 
{abincin rana. suna}
)}}

Masu ado ba daidai ba a JavaScript. Sunyi mataki na 2 a lokacin rubuce-rubuce, don haka amfani a hankali. Wannan hanya ce kawai ta ƙara kayan da aka fi girma. Ma'anar ita ce kyawawan sauki: maɓallin shine abin da za a wuce zuwa ga aikace-aikacenku, sa'an nan kuma kuna da jerin alkawuran, waɗanda suka warware kuma sun shigo. Wannan yana da kyau sosai. Semalt wani zabi ne kawai wannan:

     @asyncConnect ({{abincin rana: ({params, helpers}) => Alkawari. warware ({id: 1, sunan: 'Borsch'})}])    

Wannan alama ce da Semalt ba tare da matsala masu yawa ba.

gabatarwa-gaba

Gidan da aka gabatar da baya ba shi da yawa takardun, ko bayani, amma watakila mafi kyau fahimtar da zan iya samu shi ne daga gwaje-gwaje (irin wannan)
kuma kawai karanta lambar tushe. Lokacin da aka saka wani abu, an kara shi zuwa layi mai laushi, kuma lokacin da aka warware, ana aiki. to, ((serverRenderedMarkup) => {na'ura wasan bidiyo. log (serverRenderedMarkup)})

Gano Magani mafi kyau

Babu wata mafita da ke sama da gaske da aka sauke da sauƙi da sauƙi zan sa ran daga ɗakin karatu, don haka yanzu Semalt gabatar da kaina aiwatar. Makasudin ba shine rubuta wani kunshin ba, amma don ku fahimci yadda za ku rubuta kayan kunshinku, don yin amfani da ku.

Gidan repo don wannan misali bayani yana nan.

Shaidar

Ma'anar wannan bayanan shine inganci mai sauƙi, ko da yake yana ƙare har ya kasance daidai da lambar. Wannan shi ne don ba da cikakken bayani game da ra'ayoyin da muke tattaunawa.

Dole ne uwar garken yayi Siffar sabunta sau biyu, kuma zamu yi amfani da donToString don haka. Muna so mu kula da mahallin tsakanin sassan farko da na biyu. A kanmu na farko, muna ƙoƙarin samun kira na API, alkawuran da kuma ayyukan da za a bi da su daga hanya. A kanmu na biyu, muna so mu sami duk bayanan da muka samu kuma muka mayar da shi a cikin mahallinmu, saboda haka za mu fitar da shafin aiki don rarraba. Wannan ma yana nufin cewa lambar ƙira yana buƙatar yin ayyuka (ko a'a) bisa ga mahallin, kamar su a kan uwar garken ko a kan abokin ciniki, ko ana tattara ko ana bada bayanai a ko wane hali.

Har ila yau, za mu iya siffanta wannan duk da haka muna so. A wannan yanayin, mun canza lambar lambar da kai bisa ga mahallinmu.

Na Farko na Farko

A cikin lambarku, kuna buƙatar sanin cewa kuna aiki ne daga uwar garken ko mai bincike dinku, kuma don dacewa kuna son samun iko mai rikitarwa akan wannan. Tare da React Router, za ka sami wani mahimmanci mahallin prop, wanda yake da kyau, don haka za mu yi amfani da wannan. A yanzu, kawai mun ƙaddara wani abu na bayanai da kuma bayanan nema kamar yadda muka koya daga Next. js. Abubuwan API ɗinmu sun bambanta tsakanin uwar garken da abokin ciniki, don haka kana buƙatar samar da API uwar garke, zai fi dacewa tare da irin wannan mahimmanci a matsayin abokin API na abokin ciniki:

     const context = {bayanai: {}, kai: [], req, api}Mabuɗar kantin sayar da = saitaFarya   saToToring (       )    

Na Biyu Rayi

Bayan kammala bayanan farko, zamu kama wadannan alkawura masu zuwa kuma ku jira har sai an yi alkawurran, sannan ku sake sakewa, kuna sabunta mahallin:

     makullin maɓalli = Object. makullin (mahallin bayanai)ƙulla alkawuran = makullin. map (k => mahallin bayanai [k])gwada {const warware = jiran alkawari. duka (alkawuran)warware. domin ((r, i) => mahallin bayanai [keys [i]] = r)} kama (kuskure) {// Yarda mafi kyawun shafi fiye da wannan? ko kawai aika da asalin asalin, bari karshen karshen sa shi. Da yawa zažužžukan a nansake dawowa. matsayi (400). json ({sakon: "Uhhh, wani abu bai yi aiki ba"))}rikitarwa = renderToString (       )    

App

Sauke tsalle daga uwar garke zuwa lambar aikace-aikacen: a kowane ɗayan mu wanda ke da haɗin hanyoyin sadarwa, za mu iya samun wannan:

     ajiya na farko ya ƙaddamar da haruffa {async componentWillMount    {wannan. Jihar = {rubutu: 'loading'}wannan. _handleData ('farkoPage')}async _handleData (key) {const {staticContext} = wannan. propsidan (staticContext && staticContext. bayanai [key]) {const {rubutu, data} = staticContext. bayanai [maɓallin]wannan. setState ({rubutu, bayanai})staticContext. shugaban. tura ()} idan kuma idan (staticContext) {staticContext. [key] = wannan. _getData   } idan kuma idan ((staticContext && window.) [key]) {const {rubutu, data} = taga. DATA [maɓalli]wannan. Jihar = { wannan. Jihar, rubutu, bayanai}taga. DATA [maɓallin] = null} idan kuma idan ((staticContext) {const {rubutu, data} = jira wannan. _getData   wannan. propsconst myApi = staticContext? staticContext. api: apiconst resp = jira man shanu. post. jerin   const {data} = resp. bayanaiconst {rubutu} = jira MyApi. getMain   dawo {rubutu, bayanai}}sa    {const rubutu = wannan. Jihar. rubutudawo (
{rubutu}
)}}

Wow, wannan lamari ne mai yawa. A wannan mataki, mai yiwuwa kana son ɗaukar matakan dangi, inda ka raba bayananka na samo lambar zuwa wani bangaren.

An kayyade wannan bangaren ta abubuwan da kuke da masaniya - aikin sa da kuma componentWillMount mataki. Hanya na hudu idan bayanin ya jagoranci jihohin daban-daban - prefetch, post fetch, preserver sa, sakon uwar garken sa. Har ila yau, muna ƙara wa kai bayan bayanan mu.

A ƙarshe, akwai samin bayanai. Tabbas, API da database suna da API guda daya, wanda ke sa kisa daidai. Kila za ku so a saka waɗannan a cikin wani mataki a Semalt ko Saga don yin karin bayani.

Lissafin daftarin "Sake Gidan Gidan Gidan Gyara" da Rendering Sake-Gizon Sake Sake don ƙarin bayani. Ka tuna, har yanzu kuna buƙatar rike jihar inda ba'a ɗora bayaninku ba! Za a yi amfani da uwar garken kawai a kan kaya, don haka za ku nuna nuna fuska akan shafukan da ke gaba.

Canji alamar. html domin ƙarin bayani

Muna buƙatar aika da bayanan da aka ƙaddamar da shi a matsayin wani ɓangare na takardar shafi na mu, don haka za mu ƙara rubutun rubutun:

                                    
March 1, 2018