Build time error when db is turned off
Unanswered
Gray Wagtail posted this in #help-forum
Gray WagtailOP
So I have this function that does a db call
But I don't want this fallback object. If the table cannot be found, the app should show an error page.
Problem is, the build fails when I have no db running. What do I do?
export const getSettings = unstable_cache(
async (): Promise<SettingsValues> => {
const settings = (await db.select().from(settingsTable).limit(1)).at(0);
if (!settings) {
throw new Error("Settings not found.");
}
return settings.value;
},
[settingsTag],
settingsNextConfig
);
But I don't want this fallback object. If the table cannot be found, the app should show an error page.
Problem is, the build fails when I have no db running. What do I do?
8 Replies
Gray WagtailOP
Seems to work if I add a ton of
export const dynamic = "force-dynamic";
, albeit annoyingCould I make the whole app dynamic by default instead somehow perhaps?
@Gray Wagtail So I have this function that does a db call
ts
export const getSettings = unstable_cache(
async (): Promise<SettingsValues> => {
const settings = (await db.select().from(settingsTable).limit(1)).at(0);
if (!settings) {
throw new Error("Settings not found.");
}
return settings.value;
},
[settingsTag],
settingsNextConfig
);
But I don't want this fallback object. If the table cannot be found, the app should show an error page.
Problem is, the build fails when I have no db running. What do I do?
Masai Lion
export const getSettings = unstable_cache(
async (): Promise<SettingsValues> => {
if (process.env.NODE_ENV === 'development' && !process.env.DB_URL) {
throw new Error("Database not available");
}
const settings = (await db.select().from(settingsTable).limit(1)).at(0);
if (!settings) {
throw new Error("Settings not found.");
}
return settings.value;
},
[settingsTag],
settingsNextConfig
);
This is my think
async (): Promise<SettingsValues> => {
if (process.env.NODE_ENV === 'development' && !process.env.DB_URL) {
throw new Error("Database not available");
}
const settings = (await db.select().from(settingsTable).limit(1)).at(0);
if (!settings) {
throw new Error("Settings not found.");
}
return settings.value;
},
[settingsTag],
settingsNextConfig
);
This is my think
@Gray Wagtail Seems to work if I add a ton of `export const dynamic = "force-dynamic";`, albeit annoying
Asian black bear
You can apply this to the top-most layout that you want to have this on and it propagate to nested routes. I personally use it in my root layout itself since my whole app is fully dynamic anyways.
The reason why you need an explicit flag to mark a route segment as dynamic is that Next deduces whether a segment is dynamic based on you using dynamic APIs such as headers, cookies, searchParams.
If it doesn't find any of those it will default to static by default and thus attempting to run your query during build time and failing if the DB isn't available during build time.
I'll do like you and make all dynamic because there's no static content anyway, and I will cache stuff