前提・実現したいこと
next.js + firebase hosting + cloud function でPWAを実現したい
発生している問題・エラーメッセージ
こちらのnext.js+firebase hostingにservice-workerを追加してPWA化をしようとしています。しかし、hostingまでは動くのですが、service-worker.jsの実行が上手くいかず困っています。
https://github.com/zeit/next.js/tree/canary/examples/with-firebase-hosting
登録までの流れとしては
①nextのビルド時にworkboxを使ってservice-worker.jsファイルを作成して、
firebase hosting へデプロイを行うfunctionsフォルダ配下へnextフォルダごとコピー
②デプロイ後、_document.js内で、
navigator.serviceWorker.register('/service-worker')
を実行して/service-workerへfetch
③ /service-workerへのfetchをcloud functionで検知して、
service-worker.jsを実行
以上の流れになっています。
しかし、404エラーで登録に失敗しています。
おそらく、service-worker.jsのパスが上手く指定できていないことが原因だと思われますが、どのようにしたら登録が可能になるかお聞きしたいです。
エラーメッセージ Service worker registration failed, error1: TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.
該当のソースコード
ソースコード (functions/index.js) const functions = require('firebase-functions'); const next = require('next'); import * as admin from 'firebase-admin'; const { join } = require('path'); const { parse } = require('url'); const express = require('express'); const routes = require('./routes'); // Region hosting next.js to cloud function const dev = process.env.NODE_ENV !== 'production'; const app = next({ dev, conf: { distDir: 'next' } }); const handle = routes.getRequestHandler(app); const server = express(); server.get('*', (req, res) => handle(req, res)); server.get('/service-worker', (req, res) => { // いくつかパスを書き換えて試してみましたが、いづれもうまく動きませんでした。 // app.serveStatic(req, res, '_next/service-worker.js'), // app.serveStatic(req, res, 'next/service-worker.js'); // app.serveStatic(req, res, '/service-worker.js'); // app.serveStatic(req, res, '/next/service-worker.js'); const filePath = join(__dirname , 'service-worker.js') app.serveStatic(req, res, filePath); }); exports.next = functions.https.onRequest((req, res) => { console.log('File: ' + req.originalUrl); // log the page.js file that is being requested return app.prepare().then(() => { server(req, res); }); });
試したこと
プロジェクトの構成(firebase hosting へデプロイされるdistフォルダと、build前のフォルダ構成)
json
1firebase.jsonの設定 2{ 3 "hosting": { 4 "public": "dist/public", 5 "ignore": [ 6 "firebase.json", 7 "**/.*", 8 "**/node_modules/**" 9 ], 10 "rewrites": [ 11 { 12 "function": "next", 13 "source": "**/**" 14 } 15 ], 16 "predeploy": "npm run build-public" 17 }, 18 "functions": { 19 "source": "dist/functions", 20 "predeploy": 21 "npm run build-funcs && npm run build-app && npm run copy-deps && npm run install-deps" 22 } 23} 24
補足情報(FW/ツールのバージョンなど)
"next": "^6.1.1", "next-offline": "^2.8.0", "node": ">=8.1.4", "npm": ">=5.4.2" "next-routes": "^1.4.2",
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/25 13:18