Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel_Nuxtjsでの構成とつまづきポイント

kotamat
November 24, 2017

 Laravel_Nuxtjsでの構成とつまづきポイント

Laravel Vue.js勉強会#2にて発表した内容です。

kotamat

November 24, 2017
Tweet

More Decks by kotamat

Other Decks in Programming

Transcript

  1. ૒ํͱ΋ಉҰϦϙͰ؅ཧ |-- app |-- artisan |-- bootstrap |-- composer.json |--

    composer.lock |-- config |-- database |-- phpunit.xml |-- public |-- resources |-- routes |-- server.php |-- storage |-- tests |-- vendor |-- client |-- node_modules |-- nuxt.config.js |-- package.json `-- yarn.lock
  2. ૒ํͱ΋ಉҰϦϙͰ؅ཧ |-- app |-- artisan |-- bootstrap |-- composer.json |--

    composer.lock |-- config |-- database |-- phpunit.xml |-- public |-- resources |-- routes |-- server.php |-- storage |-- tests |-- vendor |-- client |-- node_modules |-- nuxt.config.js |-- package.json `-- yarn.lock client༻ͷϑΝΠ ϧ͸ҰՕॴʹ·ͱ ΊΔ
  3. /client഑ԼʹNuxtϓϩδΣΫτ client/ |-- assets |-- components |-- css |-- layouts

    |-- middleware |-- mixins |-- pages |-- plugins |-- static |-- store `-- utils
  4. NginxͰlocationʹΑΔ෼ذ server { location /api/ { root <σϓϩΠઌDir>/public/; index index.php;

    try_files $uri $uri/ /api/index.php?$query_string; location ~ \.php$ { ... } } location / { alias <σϓϩΠઌDir>/dist/; index index.html; try_files $uri $uri/ /index.html = 404; } }
  5. NginxͰlocationʹΑΔ෼ذ server { location /api/ { root <σϓϩΠઌDir>/public/; index index.php;

    try_files $uri $uri/ /api/index.php?$query_string; location ~ \.php$ { ... } } location / { alias <σϓϩΠઌDir>/dist/; index index.html; try_files $uri $uri/ /index.html = 404; } } /apiͷϦΫΤετ ͸LaravelΛಡΈʹ ߦ͘
  6. NginxͰlocationʹΑΔ෼ذ server { location /api/ { root <σϓϩΠઌDir>/public/; index index.php;

    try_files $uri $uri/ /api/index.php?$query_string; location ~ \.php$ { ... } } location / { alias <σϓϩΠઌDir>/dist/; index index.html; try_files $uri $uri/ /index.html = 404; } } ͦΕҎ֎͸nuxt generate͞Εͨ ϑΝΠϧΛಡΈʹ ߦ͘
  7. NginxͰlocationʹΑΔ෼ذ server { location /api/ { root <σϓϩΠઌDir>/public/; index index.php;

    try_files $uri $uri/ /api/index.php?$query_string; location ~ \.php$ { ... } } location / { alias <σϓϩΠઌDir>/dist/; index index.html; try_files $uri $uri/ /index.html = 404; } } ϑΝΠϧΛಡΈʹ ߦͬͯଘࡏ͠ͳ͚ Ε͹/index.html ͰSPA
  8. ΫϥΠΞϯτ: @nuxtjs/auth ..? • ೝূʹඞཁͳػೳΛઃఆϑΝΠϧʹهड़͢Δ͚ͩͰ࣮ݱ • ϩάΠϯɺϩάΞ΢τAPI΁ͷϦΫΤετ • औಘͨ͠τʔΫϯͷӬଓԽ(Cookie, localStorage)

    • ೝূɺඇೝূ࣌ͷϦμΠϨΫτmiddleware • nuxt_community͕ఏڙ • ͔͠͠ɺAPIͷํͰϩάΠϯϢʔβΛGET͢ΔΤϯυϙΠϯτ͕ଘࡏ͍ͯ͠Δඞཁ ͕͋ͬͨΓɺࡉ͔͍ڍಈʹؔͯ͠͸੍ޚ͕Ͱ͖ͳ͍ • ࠓճ͸ࣗ࡞
  9. nuxt generateͱnuxtίϚϯυ ͷڍಈͷ͕͍ͪ • `nuxt`ίϚϯυͷ৔߹: • Ϗϧυ࣌ʹಡΈࠐΉϑΝΠϧશମʹbabelΛదԠͯ͘͠ΕΔɻ • `nuxt generate`ίϚϯυͷ৔߹:

    • Nuxt.jsϓϩδΣΫτ഑ԼҎ֎ͷϑΝΠϧ(node_module಺౳)ͰES6ܗࣜͷ JSͩͱΤϥʔ • ίϯϙωϯτΛimport͢Δͱ͜ΖͰେจࣈͷϑΝΠϧ໊Λࢦఆ͠ɺ࣮ࡍͷ ϑΝΠϧ໊͕খจࣈͩͱΤϥʔ →`nuxt generate`ίϚϯυ΋CIͰνΣοΫ
  10. 1.0.0-rc3Ͱͷมߋ఺ͱυΩϡϝ ϯτ • Aliases are now ~, ~~, @, @@,

    assets and static (~plugins should be renamed to ~/ plugins for example) • Programmatic Usage is different now, see custom-server example and updated express template • build.loaders has been removed in nuxt.config.js, use build.extend instead • injectAs has been removed in plugins, you can inject your plugins by accessing app • process.BROWSER_BUILD has been removed in flavour of process.browser • process.SERVER_BUILD has been removed in flavour of process.server • Remove ~store and ~router aliases, use plugins to access router and store →1.0·Ͱ͸APIมߋͳ͍͕ɺҰԠϦϦʔεϊʔτ͸ݟΑ͏
  11. SSRͷͭΒΈ • SSRͰͷΈൃੜ͢ΔΤϥʔΛͨͲΔͷ͕େม(ϒϥ΢βͷσϕϩούʔ πʔϧʹ͸දࣔ͞Εͳ͍) • nuxt/youch(Nuxt७ਖ਼ΤϥʔϨϙʔςΟϯά)Ͱ͸Χόʔ͖͠Εͳ͍࣌ ͕͋Δ • 1.0ʹ্͕ΔλΠϛϯάͰSSRϞʔυͷOn Off͸બ୒Ͱ͖ΔΑ͏ʹ

    • ҰԠݱঢ়Ͱ΋`nuxt —spa`ͰΫϥΠΞϯτͷΈϏϧυ͸Մೳ (> rc7 ) • process.mode = ‘spa’Ͱ΋spaϞʔυͰ࣮ߦՄೳ ( > rc7 ) →nuxt generate || <no-ssr/> || spaϞʔυ
  12. localStorage vs Cookie • Node.jsαʔόʔΛ࢖༻͢Δ৔߹͸ localStorage͸࢖༻ग़དྷͳ͍ͷͰɺCookieʹ σʔλΛอଘ͢Δඞཁ͕͋Δ import JsCookie from

    'js-cookie' import Cookie from ‘cookie' … if (process.client) { window.localStorage.setItem('access_token', authData.access_token) JsCookie.set('access_token', authData.access_token) } else { this.$context.res.setHeader('Set-Cookie', Cookie.serialize('access_token', authData.access_token)) }
  13. Credential • CookieΛผυϝΠϯ͔ΒLaravelʹૹ৴͍ͨ͠৔߹͸ CORSͷઃఆ͕ඞཁ • barryvdh/laravel-cors Λ࢖͏৔߹͸࠷௿ݶԼهͷΑ͏ʹઃ ఆ return [

    - 'supportsCredentials' => false, - 'allowedOrigins' => ['*'], + 'supportsCredentials' => true, + 'allowedOrigins' => [ + env('APP_URL'), + ], 'allowedHeaders' => ['*'], 'allowedMethods' => ['*'], 'exposedHeaders' => [], 'maxAge' => 0, ];