-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
オフラインでも時間割を確認できるように #601
オフラインでも時間割を確認できるように #601
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
||
const CACHE_NAME = "TwinteFallback"; | ||
const VERSIONS = ["v1"]; | ||
|
||
let base_url: string; | ||
|
||
if (process.env.NODE_ENV === "development") { | ||
base_url = process.env.BASE_URL || "http://localhost:4000"; | ||
} else { | ||
base_url = process.env.BASE_URL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: 本番ビルド時に環境変数を注入するように設定をする Production で dotenv (に限らずコミットされるファイルに環境値がかかれたもの)を使用するのは微妙だと思うため。 |
||
} | ||
|
||
declare const self: ServiceWorkerGlobalScope; | ||
|
||
self.addEventListener("install", () => { | ||
VERSIONS.slice(0, Math.max(0, VERSIONS.length - 2)).forEach( | ||
async (version) => { | ||
await caches.delete(getCacheName(CACHE_NAME, version)); | ||
} | ||
); | ||
}); | ||
|
||
self.addEventListener("fetch", (event) => { | ||
const request = event.request; | ||
|
||
if (request.method !== "GET") return; | ||
if ( | ||
!( | ||
request.url.startsWith(base_url) || | ||
request.url.startsWith("https://fonts.googleapis.com/") || | ||
request.url.startsWith("https://fonts.gstatic.com/") | ||
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Google Fonts がないとオフライン時にアイコンが表示されないのでキャッシュをしている。 |
||
) | ||
) | ||
return; | ||
|
||
event.respondWith(getResponseWithCaching(request)); | ||
}); | ||
|
||
const getCacheName = (name: string, version: string) => `${name}_${version}`; | ||
|
||
const getResponseWithCaching = async (request: Request): Promise<Response> => { | ||
const cache = await caches.open( | ||
getCacheName(CACHE_NAME, VERSIONS[VERSIONS.length - 1]) | ||
); | ||
|
||
let response: Response | undefined; | ||
|
||
const hasCredentials = request.url.startsWith(base_url); | ||
|
||
try { | ||
response = await fetch( | ||
request, | ||
hasCredentials | ||
? { | ||
credentials: "include", | ||
} | ||
: {} | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
response = await cache.match(request.url); | ||
if (!response) throw new Error("There is no cache"); | ||
} | ||
|
||
if (response.ok) cache.put(request, response.clone()); | ||
|
||
return response; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"include": ["./**/*.ts"], | ||
"compilerOptions": { | ||
"sourceMap": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"preserveValueImports": false, | ||
"esModuleInterop": true, | ||
"importsNotUsedAsValues": "remove", | ||
"baseUrl": ".", | ||
"paths": { | ||
"~/*": ["./*"] | ||
}, | ||
"lib": ["ES2021", "WebWorker"], | ||
"types": ["node"], | ||
"outDir": "../dist" | ||
} | ||
} | ||
Comment on lines
+1
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
例えば |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
</template> | ||
|
||
<script setup lang="ts"> | ||
import { onErrorCaptured, onMounted, ref } from "vue"; | ||
import { onErrorCaptured, ref } from "vue"; | ||
import { useRouter } from "vue-router"; | ||
import { | ||
InternalServerError, | ||
|
@@ -32,14 +32,7 @@ setSetting(); | |
|
||
const router = useRouter(); | ||
|
||
/** unregister service worker (v2) */ | ||
onMounted(() => { | ||
navigator.serviceWorker.getRegistrations().then(function (registrations) { | ||
for (let registration of registrations) { | ||
registration.unregister(); | ||
} | ||
}); | ||
}); | ||
Comment on lines
-35
to
-42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v2 公開終了から 1 年半が経過したので流石にもういらないと思われる。 |
||
navigator.serviceWorker.register("/fallback.js"); | ||
|
||
/** error */ | ||
const errorMessage = ref(""); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/tsconfig.ts
に"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
が指定されていることからわかるように、typecheckOnlyStaged
は/src
以下の型検査のみに関心を持っている。したがってここでは/src
以下を検査するようにして/serviceworkers
以下は除外している。なお
yarn typecheck
で/serviceworkers
以下の型検査はちゃんと走る。