Skip to content

Commit

Permalink
Improve proxy, missing code from last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Nov 19, 2024
1 parent c26f77c commit ad15951
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
47 changes: 37 additions & 10 deletions apps/rr7/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
ScrollRestoration,
useHref,
useLocation,
useNavigate,
useNavigate as useRRNavigate,
useParams,
useLoaderData,
} from 'react-router';
import type { LinksFunction } from 'react-router';

Expand All @@ -19,11 +20,21 @@ import { PloneProvider } from '@plone/providers';
import { flattenToAppURL } from './utils';
import config from '@plone/registry';
import install from './config';
import installSSR from './config.server';

install();

import '@plone/theming/styles/main.css';
import '@plone/slots/main.css';

install();
function useNavigate() {
const navigate = useRRNavigate();
return (to: string) => navigate(flattenToAppURL(to));
}

function useHrefLocal(to: string) {
return useHref(flattenToAppURL(to));
}

export const links: LinksFunction = () => [
{ rel: 'preconnect', href: 'https://fonts.googleapis.com' },
Expand All @@ -38,7 +49,20 @@ export const links: LinksFunction = () => [
},
];

export async function loader() {
const ssrConfig = installSSR();

return {
env: {
PLONE_API_PATH: ssrConfig.settings.apiPath,
PLONE_INTERNAL_API_PATH: ssrConfig.settings.internalApiPath,
},
};
}

export function Layout({ children }: { children: React.ReactNode }) {
const data = useLoaderData<typeof loader>();

return (
<html lang="en">
<head>
Expand All @@ -50,13 +74,23 @@ export function Layout({ children }: { children: React.ReactNode }) {
<body>
{children}
<ScrollRestoration />
<script
dangerouslySetInnerHTML={{
__html: `window.env = ${JSON.stringify(data.env)}`,
}}
/>
<Scripts />
</body>
</html>
);
}

export default function App() {
if (!import.meta.env.SSR) {
config.settings.apiPath = window.env.PLONE_API_PATH;
config.settings.internalApiPath = window.env.PLONE_INTERNAL_API_PATH;
}

const [queryClient] = useState(
() =>
new QueryClient({
Expand All @@ -76,14 +110,7 @@ export default function App() {
}),
);

const RRNavigate = useNavigate();
const navigate = (to: string) => {
return RRNavigate(flattenToAppURL(to));
};

function useHrefLocal(to: string) {
return useHref(flattenToAppURL(to));
}
const navigate = useNavigate();

return (
<PloneProvider
Expand Down
14 changes: 12 additions & 2 deletions apps/rr7/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import tsconfigPaths from 'vite-tsconfig-paths';
import { defineConfig } from 'vite';
import { PloneRegistryVitePlugin } from '@plone/registry/vite-plugin';

const prodServerName =
process.env.PLONE_API_PATH && process.env.PLONE_API_PATH.startsWith('https')
? process.env.PLONE_API_PATH
: '';

export default defineConfig({
plugins: [
reactRouter({
Expand All @@ -16,8 +21,13 @@ export default defineConfig({
port: 3000,
proxy: {
'^/\\+\\+api\\+\\+($$|/.*)': {
target:
'http://localhost:8080/VirtualHostBase/http/localhost:3000/Plone/++api++/VirtualHostRoot',
target: prodServerName
? prodServerName
: 'http://localhost:8080/VirtualHostBase/http/localhost:3000/Plone/++api++/VirtualHostRoot',
...(prodServerName && {
changeOrigin: true,
secure: false,
}),
rewrite: (path) => {
console.log(path);
return path.replace('/++api++', '');
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/Teaser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Link } from '@plone/components';

const TeaserBlockView = (props: BlockViewProps) => {
const { data } = props;
const href = Array.isArray(data.href) ? data.href[0]['@id'] : data.href;
const href = Array.isArray(data.href) ? data.href?.[0]?.['@id'] : data.href;
const image = data.preview_image?.[0];
// TODO: Improve the images download path including the ++api++ to avoid having to setup a redirect
// for @@images and @@download
Expand Down

0 comments on commit ad15951

Please sign in to comment.