Skip to content

Commit

Permalink
fix: dynamic mui broken after build
Browse files Browse the repository at this point in the history
  • Loading branch information
Dabolus committed Apr 25, 2024
1 parent 9fb6b7e commit 3d47623
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
5 changes: 3 additions & 2 deletions app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import createTheme from './src/theme';
import PageStyles from './src/PageStyles';
import Backend from './i18next-fetch-backend';
import i18n from './i18n';
import { muiLicenseKey } from './muiLicenseHandler';

if (import.meta.env.VITE_MUI_LICENSE_KEY) {
LicenseInfo.setLicenseKey(import.meta.env.VITE_MUI_LICENSE_KEY);
if (muiLicenseKey) {
LicenseInfo.setLicenseKey(muiLicenseKey);
}

interface ClientCacheProviderProps {
Expand Down
19 changes: 15 additions & 4 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import createTheme from './src/theme';
import PageStyles from './src/PageStyles';
import i18next, { localesDirectory } from './i18next.server';
import i18n from './i18n';

if (import.meta.env.VITE_MUI_LICENSE_KEY) {
LicenseInfo.setLicenseKey(import.meta.env.VITE_MUI_LICENSE_KEY);
}
import { muiLicenseKey } from './muiLicenseHandler';

// Reject all pending promises from handler functions after 5 seconds
export const streamTimeout = 5000;
Expand Down Expand Up @@ -115,6 +112,20 @@ export default async function handleRequest(

responseHeaders.set('Content-Type', 'text/html');

if (muiLicenseKey) {
LicenseInfo.setLicenseKey(muiLicenseKey);
responseHeaders.set(
'Set-Cookie',
`mui-license-key=${muiLicenseKey}; Path=/`,
);
} else {
// Clear the license key cookie if it was removed for some reason, just in case
responseHeaders.set(
'Set-Cookie',
'mui-license-key=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT',
);
}

return new Response(markup, {
status: responseStatusCode,
headers: responseHeaders,
Expand Down
4 changes: 4 additions & 0 deletions app/muiLicenseHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const muiLicenseKey =
'document' in globalThis
? document.cookie.match(/(?:^|;) ?mui-license-key=([^;]*)(?:;|$)/)?.[1]
: process?.env?.MUI_LICENSE_KEY;
7 changes: 4 additions & 3 deletions app/src/components/DataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { muiLicenseKey } from '~/muiLicenseHandler';
import { DataGrid as DataGridBase } from '@mui/x-data-grid';
import { DataGridPro } from '@mui/x-data-grid-pro';
export type * from '@mui/x-data-grid';

export const DataGrid = import.meta.env.VITE_MUI_LICENSE_KEY
? await import('@mui/x-data-grid-pro').then(imp => imp.DataGridPro)
: await import('@mui/x-data-grid').then(imp => imp.DataGrid);
export const DataGrid = muiLicenseKey ? DataGridPro : DataGridBase;

export default DataGrid;
2 changes: 0 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export default defineConfig(({ mode }) => ({
],
build: {
sourcemap: true,
// Browsers with top-level await support
target: ['chrome89', 'edge89', 'firefox89', 'safari15', 'opera75'],
rollupOptions: {
onwarn(warning, warn) {
// Ignore warnings related to "use client" directives
Expand Down

0 comments on commit 3d47623

Please sign in to comment.