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

[FEAAS] Nextjs Image for FEAAS #1754

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"dependencies": {
"@sitecore/components": "^1.1.3",
"@sitecore-cloudsdk/events": "^0.1.1",
"@sitecore-feaas/clientside": "^0.5.0"
"@sitecore-feaas/clientside": "^0.5.17"
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// The BYOC bundle imports external (BYOC) components into the app and makes sure they are ready to be used
import BYOC from 'src/byoc';
import CdpPageView from 'components/CdpPageView';
import FEAASScripts from 'components/FEAASScripts';

const Scripts = (): JSX.Element => {
return (
<>
<BYOC />
<CdpPageView />
<FEAASScripts />
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ import './index.hybrid';
// As long as component bundle is exported and rendered on page (as an empty element), client-only BYOC components are registered and become available
// The rest of components will be regsitered in both server and client-side contexts when this module is imported into Layout
FEAAS.enableNextClientsideComponents(dynamic, ClientBundle);

export default FEAAS.ExternalComponentBundle;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Image from 'next/image';
import * as FEAAS from '@sitecore-feaas/clientside/react';
import nextConfig from 'next.config';
// Element implementations for Sitecore Component Builder can be overriden here

const FEAASScripts = (): JSX.Element => {
// we cannot use nextjs's logic for remotePatterns matching without extra dependencies
// so we use a limited approach for now - which will be replaced once nextjs allows to fall back to unoptimized OOB
const shouldOptimize = (src: string) => {
if (src.startsWith('http')) {
const url = new URL(src);
const domains: string[] = nextConfig().images?.domains || [];
ambrauer marked this conversation as resolved.
Show resolved Hide resolved
return (
domains.some((domain) => url.hostname === domain) ||
url.hostname.match(/feaas\S*\.blob\.core\.windows\.net/)
);
}
return true;
};

// Register next Image to be used in Component Builder.
// Nextjs image implementation will be used when img is rendered in component from Component Builder
FEAAS.setElementImplementation('img', (attributes: Record<string, string>) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { children, src, alt, ...imgAttributes } = attributes;
return (
<Image
height={1920}
width={1200}
unoptimized={!shouldOptimize(src)}
src={src}
alt={alt}
{...imgAttributes}
/>
);
});

return <></>;
};

export default FEAASScripts;
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const nextConfig = {
hostname: 'feaas*.blob.core.windows.net',
port: '',
},
],
]
},

async rewrites() {
Expand Down