Skip to content

Commit

Permalink
Improve deployment settings, add apiPath/internalApiPath detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Nov 18, 2024
1 parent d356de3 commit 53cec1b
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 57 deletions.
2 changes: 1 addition & 1 deletion apps/remix/app/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ploneClient from '@plone/client';
import config from '@plone/registry';

const cli = ploneClient.initialize({
apiPath: config.settings.apiPath,
apiPath: config.settings.internalApiPath || config.settings.apiPath,
});

export { cli as ploneClient };
17 changes: 12 additions & 5 deletions apps/remix/app/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import config from '@plone/registry';
import applyAddonConfiguration from '@plone/registry/addons-loader';

Check failure on line 2 in apps/remix/app/config.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unable to resolve path to module '@plone/registry/addons-loader'

applyAddonConfiguration(config);

config.settings.apiPath = 'http://localhost:3000';

export default config;
export default function install() {
applyAddonConfiguration(config);
config.settings.apiPath =
process.env.PLONE_API_PATH || 'http://localhost:3000';
config.settings.internalApiPath = process.env.PLONE_INTERNAL_API_PATH || '';
console.log('API_PATH is:', config.settings.apiPath);
console.log(
'INTERNAL_API_PATH is:',
config.settings.internalApiPath || 'not set',
);
return config;
}
12 changes: 6 additions & 6 deletions apps/remix/app/routes/_index.tsx → apps/remix/app/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
useQueryClient,
useQuery,
} from '@tanstack/react-query';
import { flattenToAppURL } from '../utils';
import { flattenToAppURL } from './utils';
import { useLoaderData, useLocation } from '@remix-run/react';
import { usePloneClient } from '@plone/providers';
// import { Breadcrumbs, RenderBlocks } from '@plone/components';
// import config from '@plone/registry';
import { ploneClient } from '../client';
import { ploneClient } from './client';
import App from '@plone/slots/components/App';

export const meta: MetaFunction = () => {
Expand All @@ -25,7 +25,7 @@ export const meta: MetaFunction = () => {
];
};

const expand = ['breadcrumbs', 'navigation'];
const expand = ['navroot', 'breadcrumbs', 'navigation'];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
Expand All @@ -42,16 +42,16 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => {
const { getContentQuery } = ploneClient;

await queryClient.prefetchQuery(
getContentQuery({ path: flattenToAppURL(request.url), expand }),
getContentQuery({ path: flattenToAppURL(request.url || '/'), expand }),
);

return json({ dehydratedState: dehydrate(queryClient) });
};

function Page() {
const { getContentQuery } = usePloneClient();
const pathname = useLocation().pathname;
const { data } = useQuery(getContentQuery({ path: pathname, expand }));
const { pathname } = useLocation();
const { data } = useQuery(getContentQuery({ path: pathname || '/', expand }));

if (!data) return null;
return (
Expand Down
5 changes: 3 additions & 2 deletions apps/remix/app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨
* For more information, see https://remix.run/file-conventions/entry.client
*/

import install from './config';
import { RemixBrowser } from '@remix-run/react';
import { startTransition, StrictMode } from 'react';
import { hydrateRoot } from 'react-dom/client';
import './config';

install();

startTransition(() => {
hydrateRoot(
Expand Down
4 changes: 3 additions & 1 deletion apps/remix/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import { createReadableStreamFromReadable } from '@remix-run/node';
import { RemixServer } from '@remix-run/react';
import { isbot } from 'isbot';
import { renderToPipeableStream } from 'react-dom/server';
import './config';
import install from './config';

const ABORT_DELAY = 5_000;

install();

export default function handleRequest(
request: Request,
responseStatusCode: number,
Expand Down
10 changes: 6 additions & 4 deletions apps/remix/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ import '@plone/components/dist/basic.css';
import '@plone/slots/main.css';
import { flattenToAppURL } from './utils';
import { PloneProvider } from '@plone/providers';

import config from '@plone/registry';

export const links: LinksFunction = () => [
...(cssBundleHref ? [{ rel: 'stylesheet', href: cssBundleHref }] : []),
];

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

export default function App() {
const [queryClient] = useState(
() =>
Expand All @@ -56,6 +53,10 @@ export default function App() {
return RRNavigate(flattenToAppURL(to));
};

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

return (
<html lang="en">
<head>
Expand All @@ -72,6 +73,7 @@ export default function App() {
useParams={useParams}
useHref={useHrefLocal}
navigate={navigate}
flattenToAppURL={flattenToAppURL}
>
<Outlet />
<ScrollRestoration />
Expand Down
2 changes: 0 additions & 2 deletions apps/remix/app/routes/$.tsx

This file was deleted.

10 changes: 6 additions & 4 deletions apps/remix/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import config from '@plone/registry';
*/
export function flattenToAppURL(url: string) {
const { settings } = config;

return (
const result =
url &&
url.replace(settings.apiPath, '').replace('http://localhost:3000', '')
);
url
.replace(settings.apiPath, '')
.replace(settings.internalApiPath || '', '')
.replace('http://localhost:3000', '');
return result;
}
8 changes: 0 additions & 8 deletions apps/remix/remix.config.js

This file was deleted.

20 changes: 20 additions & 0 deletions apps/remix/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ export default defineConfig({
},
plugins: [
remix({
routes: async (defineRoutes) => {
// If you need to do async work, do it before calling `defineRoutes`, we use
// the call stack of `route` inside to set nesting.

return defineRoutes((route) => {
route('/', 'content.tsx', { id: 'index' });
route('*', 'content.tsx', { id: 'splat' });
// A common use for this is catchall routes.
// - The first argument is the React Router path to match against
// - The second is the relative filename of the route handler
// route("/some/path/*", "catchall.tsx");

// if you want to nest routes, use the optional callback argument
// route("some/:path", "some/route/file.js", () => {
// // - path is relative to parent path
// // - filenames are still relative to the app directory
// route("relative/path", "some/other/file");
// });
});
},
future: {
v3_fetcherPersist: true,
v3_relativeSplatPath: true,
Expand Down
2 changes: 1 addition & 1 deletion apps/rr7/app/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ploneClient from '@plone/client';
import config from '@plone/registry';

const cli = ploneClient.initialize({
apiPath: config.settings.apiPath,
apiPath: config.settings.internalApiPath || config.settings.apiPath,
});

export { cli as ploneClient };
17 changes: 12 additions & 5 deletions apps/rr7/app/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import config from '@plone/registry';
import applyAddonConfiguration from '@plone/registry/addons-loader';

Check failure on line 2 in apps/rr7/app/config.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unable to resolve path to module '@plone/registry/addons-loader'

applyAddonConfiguration(config);

config.settings.apiPath = 'http://localhost:3000';

export default config;
export default function install() {
applyAddonConfiguration(config);
config.settings.apiPath =
process.env.PLONE_API_PATH || 'http://localhost:3000';
config.settings.internalApiPath = process.env.PLONE_INTERNAL_API_PATH || '';
console.log('API_PATH is:', config.settings.apiPath);
console.log(
'INTERNAL_API_PATH is:',
config.settings.internalApiPath || 'not set',
);
return config;
}
8 changes: 4 additions & 4 deletions apps/rr7/app/routes/home.tsx → apps/rr7/app/content.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { LoaderArgs } from '../routes/+types.home';
import type { LoaderArgs } from './routes/+types.home';
import {
dehydrate,
QueryClient,
HydrationBoundary,
useQuery,
useQueryClient,
} from '@tanstack/react-query';
import { flattenToAppURL } from '../utils';
import { flattenToAppURL } from './utils';
import { useLoaderData, useLocation } from 'react-router';
import { usePloneClient } from '@plone/providers';
import { ploneClient } from '../client';
import { ploneClient } from './client';
import App from '@plone/slots/components/App';
import type { MetaFunction } from 'react-router';

Expand All @@ -20,7 +20,7 @@ export const meta: MetaFunction = () => {
];
};

const expand = ['breadcrumbs', 'navigation'];
const expand = ['navroot', 'breadcrumbs', 'navigation'];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function loader({ params, request }: LoaderArgs) {
Expand Down
11 changes: 7 additions & 4 deletions apps/rr7/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ import PloneClient from '@plone/client';
import { PloneProvider } from '@plone/providers';
import { flattenToAppURL } from './utils';
import config from '@plone/registry';
import './config';
import install from './config';

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

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

export const links: LinksFunction = () => [
{ rel: 'preconnect', href: 'https://fonts.googleapis.com' },
Expand Down Expand Up @@ -83,6 +81,10 @@ export default function App() {
return RRNavigate(flattenToAppURL(to));
};

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

return (
<PloneProvider
ploneClient={ploneClient}
Expand All @@ -91,6 +93,7 @@ export default function App() {
useParams={useParams}
useHref={useHrefLocal}
navigate={navigate}
flattenToAppURL={flattenToAppURL}
>
<Outlet />
<ReactQueryDevtools initialIsOpen={false} />
Expand Down
4 changes: 2 additions & 2 deletions apps/rr7/app/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { RouteConfig } from '@react-router/dev/routes';
import { index, route } from '@react-router/dev/routes';

export const routes: RouteConfig = [
index('routes/home.tsx'),
route('*', 'routes/$.tsx'),
index('content.tsx', { id: 'index' }),
route('*', 'content.tsx', { id: 'splat' }),
];
2 changes: 0 additions & 2 deletions apps/rr7/app/routes/$.tsx

This file was deleted.

12 changes: 7 additions & 5 deletions apps/rr7/app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from './config';
import config from '@plone/registry';

/**
* Flatten to app server URL - Given a URL if it starts with the API server URL
Expand All @@ -9,9 +9,11 @@ import config from './config';
*/
export function flattenToAppURL(url: string) {
const { settings } = config;

return (
const result =
url &&
url.replace(settings.apiPath, '').replace('http://localhost:3000', '')
);
url
.replace(settings.apiPath, '')
.replace(settings.internalApiPath || '', '')
.replace('http://localhost:3000', '');
return result;
}
3 changes: 2 additions & 1 deletion packages/blocks/Image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { BlockViewProps } from '@plone/types';
import { usePloneProvider } from '@plone/providers';

const ImageBlockView = (props: BlockViewProps) => {
console.log(props);
// console.log(props);
const flattenToAppURL = usePloneProvider().flattenToAppURL;
const { data } = props;
if (!data.url) return null;
const url = data.image_scales
? `/++api++${flattenToAppURL(data.url)}/${data.image_scales[data.image_field][0].download}`
: data.url;
Expand Down

0 comments on commit 53cec1b

Please sign in to comment.