From 0029fffa83d9e3168d7f6487c2673f33f503c1e3 Mon Sep 17 00:00:00 2001 From: daedalus <44623501+ComfortablyCoding@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:24:31 -0400 Subject: [PATCH 1/4] default baseUrl to directus default --- nuxt.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nuxt.config.ts b/nuxt.config.ts index 459c51e..8c30d5c 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -52,7 +52,7 @@ export default defineNuxtConfig({ // Directus Configuration directus: { rest: { - baseUrl: process.env.DIRECTUS_URL, + baseUrl: process.env.DIRECTUS_URL || 'http://localhost:8055', nuxtBaseUrl: process.env.NUXT_PUBLIC_SITE_URL || 'http://localhost:3000', }, auth: { From 4573d96843dee4f86cbd2a0721b2092d6c8b6884 Mon Sep 17 00:00:00 2001 From: daedalus <44623501+ComfortablyCoding@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:24:43 -0400 Subject: [PATCH 2/4] add error handling for redirect and global fetch --- modules/directus/index.ts | 64 ++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/modules/directus/index.ts b/modules/directus/index.ts index 3521493..2f5d53e 100644 --- a/modules/directus/index.ts +++ b/modules/directus/index.ts @@ -160,38 +160,52 @@ export default defineNuxtModule({ const directus = createDirectus(moduleOptions.rest.baseUrl).with(rest()); // Handle Redirects - const redirects = await directus.request(readItems('redirects')); + try { + const redirects = await directus.request(readItems('redirects')); + + for (const redirect of redirects) { + let responseCode = redirect.response_code ? parseInt(redirect.response_code as any) : 301; + + if (responseCode !== 301 && responseCode !== 302) { + responseCode = 301; + } + + // Add the redirect to the route rules + // https://nuxt.com/docs/guide/concepts/rendering#route-rules + extendRouteRules(redirect.url_old as string, { + redirect: { + to: redirect.url_new, + statusCode: responseCode as 301 | 302, + }, + }); + } - for (const redirect of redirects) { - let responseCode = redirect.response_code ? parseInt(redirect.response_code as any) : 301; + log.success(`${redirects.length} Redirects loaded`); - if (responseCode !== 301 && responseCode !== 302) { - responseCode = 301; + for (const redirect of redirects) { + log.info(` • ${redirect.response_code}`, `From: ${redirect.url_old}`, `To: ${redirect.url_new}`); } - - // Add the redirect to the route rules - // https://nuxt.com/docs/guide/concepts/rendering#route-rules - extendRouteRules(redirect.url_old as string, { - redirect: { - to: redirect.url_new, - statusCode: responseCode as 301 | 302, - }, - }); + } catch (error) { + log.warn('Unable to load redirects due to the following error'); + log.error(error); + log.warn(`Please ensure the directus instance is reachable at ${moduleOptions.rest.baseUrl}.`); } - log.success(`${redirects.length} Redirects loaded`); - - for (const redirect of redirects) { - log.info(` • ${redirect.response_code}`, `From: ${redirect.url_old}`, `To: ${redirect.url_new}`); - } + try { + // Add Globals + const globals = await directus.request>(readSingleton('globals')); + nuxt.options.appConfig.globals = defu(nuxt.options.appConfig.globals, globals); + log.success('Globals loaded into appConfig'); - // Add Globals - const globals = await directus.request>(readSingleton('globals')); - nuxt.options.appConfig.globals = defu(nuxt.options.appConfig.globals, globals); - log.success('Globals loaded into appConfig'); + // Add title template to the app head for use with useHead composable + nuxt.options.app.head.titleTemplate = `%s - ${globals?.title ?? 'Agency OS'}`; + } catch (error) { + nuxt.options.app.head.titleTemplate = '%s - Agency OS'; - // Add title template to the app head for use with useHead composable - nuxt.options.app.head.titleTemplate = `%s - ${globals?.title ?? 'Agency OS'}`; + log.warn('Unable to load redirects due to the following error'); + log.error(error); + log.warn(`Please ensure the directus instance is reachable at ${moduleOptions.rest.baseUrl}.`); + } log.success(`Directus Module Loaded`); }, From 3121e1c7466789fbaf2bef0082d5d62e55c12495 Mon Sep 17 00:00:00 2001 From: daedalus <44623501+ComfortablyCoding@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:29:57 -0400 Subject: [PATCH 3/4] default directus host is `0.0.0.0` --- nuxt.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nuxt.config.ts b/nuxt.config.ts index 8c30d5c..29ba87f 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -52,7 +52,7 @@ export default defineNuxtConfig({ // Directus Configuration directus: { rest: { - baseUrl: process.env.DIRECTUS_URL || 'http://localhost:8055', + baseUrl: process.env.DIRECTUS_URL || 'http://0.0.0.0:8055', nuxtBaseUrl: process.env.NUXT_PUBLIC_SITE_URL || 'http://localhost:3000', }, auth: { From 9f6cb9b7cbb976f7f68b92cd61f21e5f17be25e2 Mon Sep 17 00:00:00 2001 From: daedalus <44623501+ComfortablyCoding@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:26:37 -0400 Subject: [PATCH 4/4] prefer localhost over 0.0.0.0 --- nuxt.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nuxt.config.ts b/nuxt.config.ts index 124ef37..1159566 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -52,7 +52,7 @@ export default defineNuxtConfig({ // Directus Configuration directus: { rest: { - baseUrl: process.env.DIRECTUS_URL || 'http://0.0.0.0:8055', + baseUrl: process.env.DIRECTUS_URL || 'http://localhost:8055', nuxtBaseUrl: process.env.NUXT_PUBLIC_SITE_URL || 'http://localhost:3000', }, auth: {