Skip to content

Commit

Permalink
Render in browser in dev mode if url (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri authored Jun 15, 2022
1 parent f172bb3 commit ae43b81
Show file tree
Hide file tree
Showing 7 changed files with 1,582 additions and 1,553 deletions.
6 changes: 6 additions & 0 deletions .changeset/big-mice-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'docusaurus-plugin-redoc': patch
'docusaurus-theme-redoc': patch
---

Fix reloads in dev mode
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-redoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default function redocPlugin(
const data: SpecProps = {
url,
themeId,
isSpecFile,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
spec: content.converted as any,
};
Expand All @@ -133,7 +134,7 @@ export default function redocPlugin(
: options.route;

const specProps = await createData(
`redocApiSpecV1.1-${options.id || '1'}.json`,
`redocApiSpecV1.2-${options.id || '1'}.json`,
JSON.stringify(data),
);
const layoutProps = await createData(
Expand Down
22 changes: 21 additions & 1 deletion packages/docusaurus-theme-redoc/src/theme/Redoc/Redoc.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React from 'react';
import clsx from 'clsx';
import '../../global';
import { Redoc as RedocComponent, RedocRawOptions } from 'redoc';
import {
Redoc as RedocComponent,
RedocStandalone,
RedocRawOptions,
} from 'redoc';
import { SpecProps } from '../../types/common';
import { useSpec } from '../../utils/useSpec';
import { ServerStyles } from './Styles';
Expand All @@ -25,6 +29,22 @@ function Redoc(
optionsOverrides,
);

const isDevMode = process.env.NODE_ENV === 'development';

if (isDevMode && specProps.isSpecFile === false) {
return (
<div
className={clsx([
'redocusaurus',
hasLogo && 'redocusaurus-has-logo',
className,
])}
>
<RedocStandalone specUrl={specProps.url} options={store.rawOptions} />
</div>
);
}

return (
<>
<ServerStyles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export function ServerStyles({
lightThemeOptions,
darkThemeOptions,
}: {
specProps: SpecProps,
lightThemeOptions: RedocRawOptions,
darkThemeOptions: RedocRawOptions,
specProps: SpecProps;
lightThemeOptions: RedocRawOptions;
darkThemeOptions: RedocRawOptions;
}) {
const fullUrl = useBaseUrl(specProps.url, { absolute: true });
const css = {
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-theme-redoc/src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type ParsedSpec = OpenAPISpec;
export interface SpecProps {
spec: ParsedSpec;
url?: string;
isSpecFile?: boolean;
themeId?: string;
}

Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-theme-redoc/src/types/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ interface SpecProps {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
spec: import('redoc/typings/types').OpenAPISpec;
url?: string;
isSpecFile?: boolean;
themeId?: string;
}

Expand Down
Loading

1 comment on commit ae43b81

@vercel
Copy link

@vercel vercel bot commented on ae43b81 Jun 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.