Skip to content
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

fix: async function as next config #252

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/default-provider/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { withNextVideo } from 'next-video/process';
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default withNextVideo(nextConfig);
export default async function config() {
return withNextVideo(nextConfig);
}

// Amazon S3 example
// export default withNextVideo(nextConfig, {
Expand Down
16 changes: 12 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { env, cwd } from 'node:process';
import { cwd } from 'node:process';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import nextConfig from 'next/config.js'
import nextConfig from 'next/config.js';
import type { NextConfig } from 'next';
// @ts-ignore
const getConfig = nextConfig.default;

Expand Down Expand Up @@ -68,7 +69,7 @@ export const videoConfigDefault: VideoConfigComplete = {
* The video config is then stored as an environment variable __NEXT_VIDEO_OPTS.
*/
export async function getVideoConfig(): Promise<VideoConfigComplete> {
let nextConfig = getConfig();
let nextConfig: NextConfig | undefined = getConfig();

if (!nextConfig?.serverRuntimeConfig?.nextVideo) {
try {
Expand All @@ -88,5 +89,12 @@ export async function getVideoConfig(): Promise<VideoConfigComplete> {
async function importConfig(file: string) {
const absFilePath = path.resolve(cwd(), file);
const fileUrl = pathToFileURL(absFilePath).href;
return (await import(/* webpackIgnore: true */ fileUrl))?.default;

const mod = await import(/* webpackIgnore: true */ fileUrl);
const config: (() => NextConfig) | NextConfig | undefined = mod?.default;

if (typeof config === 'function') {
return config();
}
return config;
}