Skip to content

Commit 7660358

Browse files
authored
Error handling for config.js (#1263)
1 parent 6795873 commit 7660358

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

resources/js/package.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ async function init() {
5555
}
5656
booting = true
5757
for (let i = 0; i < 20; i++) {
58-
// Wait until config is available, for a max of 1s
59-
if (window.config.store) {
58+
// Wait until config is available, or has thrown an error, for a max of 1s
59+
if (window.config.store || window.configError) {
6060
break
6161
}
6262
await new Promise((resolve) => setTimeout(resolve, 50))
@@ -65,7 +65,7 @@ async function init() {
6565

6666
// Check if the localstorage needs a flush.
6767
let cachekey = useLocalStorage('cachekey')
68-
if (cachekey.value !== window.config.cachekey) {
68+
if (window.config.cachekey && cachekey.value !== window.config.cachekey) {
6969
window.config.flushable_localstorage_keys.forEach((key) => {
7070
useLocalStorage(key).value = null
7171
})
@@ -152,6 +152,14 @@ async function init() {
152152
},
153153
},
154154
mounted() {
155+
window.$on('configError', () => {
156+
app.config.globalProperties.configError.value = true
157+
throw new Error('Config.js failed to load because of an error.')
158+
})
159+
if (window.configError ?? false) {
160+
window.$emit('configError')
161+
}
162+
155163
window.app.config.globalProperties.refs = this.$refs
156164
setTimeout(() => {
157165
const event = new CustomEvent('vue:mounted', { detail: { vue: window.app, rootNode: this } })
@@ -178,6 +186,7 @@ async function init() {
178186
mask: useMask(),
179187
showTax: window.config.show_tax,
180188
scrollLock: useScrollLock(document.body),
189+
configError: ref(false),
181190
// Wrap the local storage in getter and setter functions so you do not have to interact using .value
182191
guestEmail: wrapValue(
183192
useLocalStorage('email', window.debug ? 'wayne@enterprises.com' : '', { serializer: StorageSerializers.string }),

resources/views/layouts/app.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
@php($configPath = route('config') . '?v=' . Cache::rememberForever('cachekey', fn () => md5(Str::random())) . '&s=' . config('rapidez.store'))
2525
<link href="{{ $configPath }}" rel="preload" as="script">
26-
<script defer src="{{ $configPath }}"></script>
26+
<script defer src="{{ $configPath }}" onerror="window.app?.config ? window.$emit('configError') : window.configError = true"></script>
2727

2828
@vite(['resources/css/app.css', 'resources/js/app.js'])
2929
@stack('head')
@@ -32,6 +32,8 @@
3232
<body class="text antialiased has-[.prevent-scroll:checked]:overflow-clip">
3333
<div id="app" class="flex flex-col min-h-dvh">
3434
@include('rapidez::layouts.partials.global-slideover')
35+
36+
@includeWhen(config('app.debug'), 'rapidez::layouts.partials.debug')
3537
@includeWhen(!request()->routeIs('checkout'), 'rapidez::layouts.partials.header')
3638
@includeWhen(request()->routeIs('checkout'), 'rapidez::layouts.checkout.header')
3739
<main>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div
2+
v-cloak
3+
v-if="$root.configError"
4+
class="w-full bg-danger text-center text-white font-bold"
5+
>
6+
Config.js failed to load due to an error.
7+
</div>

0 commit comments

Comments
 (0)