@scalar/docusaurus 0.8.27 → 0.8.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @scalar/docusaurus
2
2
 
3
+ ## 0.8.29
4
+
5
+ ## 0.8.28
6
+
7
+ ### Patch Changes
8
+
9
+ - [#9588](https://github.com/scalar/scalar/pull/9588): Fix function-valued configuration options (like `onBeforeRequest` and the request hooks) being dropped in the Docusaurus integration. Docusaurus JSON-serializes route props, which silently strips functions, so the config is now serialized to JavaScript in the plugin and injected as a script. Exposes a reusable `serializeConfigToJs` helper from `@scalar/client-side-rendering`.
10
+
3
11
  ## 0.8.27
4
12
 
5
13
  ## 0.8.26
@@ -1,9 +1,14 @@
1
- import type { AnyApiReferenceConfiguration, CreateApiReference } from '@scalar/types';
1
+ import type { CreateApiReference } from '@scalar/types';
2
2
  import React from 'react';
3
3
  import './theme.css';
4
4
  type Props = {
5
5
  route: {
6
- configuration: AnyApiReferenceConfiguration;
6
+ /**
7
+ * The API reference configuration, pre-serialized in the plugin (Node) to a JavaScript object
8
+ * literal string. We cannot receive it as a plain object because Docusaurus JSON-serializes route
9
+ * props, which drops function-valued options like `onBeforeRequest`. See issue #6933.
10
+ */
11
+ configuration: string;
7
12
  /** Not sure where the route type is for docusaurus, couldn't find one with an ID, TODO: replace with that */
8
13
  id: string;
9
14
  };
@@ -1 +1 @@
1
- {"version":3,"file":"ScalarDocusaurus.d.ts","sourceRoot":"","sources":["../src/ScalarDocusaurus.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAErF,OAAO,KAA4B,MAAM,OAAO,CAAA;AAEhD,OAAO,aAAa,CAAA;AAEpB,KAAK,KAAK,GAAG;IACX,KAAK,EAAE;QACL,aAAa,EAAE,4BAA4B,CAAA;QAC3C,6GAA6G;QAC7G,EAAE,EAAE,MAAM,CAAA;KACX,CAAA;CACF,CAAA;AAGD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,MAAM,EAAE;YACN,kBAAkB,EAAE,kBAAkB,CAAA;SACvC,CAAA;KACF;CACF;AAED,eAAO,MAAM,gBAAgB,GAAI,WAAW,KAAK,sBAiBhD,CAAA;AAED,eAAe,gBAAgB,CAAA"}
1
+ {"version":3,"file":"ScalarDocusaurus.d.ts","sourceRoot":"","sources":["../src/ScalarDocusaurus.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAEvD,OAAO,KAAmC,MAAM,OAAO,CAAA;AAEvD,OAAO,aAAa,CAAA;AAEpB,KAAK,KAAK,GAAG;IACX,KAAK,EAAE;QACL;;;;WAIG;QACH,aAAa,EAAE,MAAM,CAAA;QACrB,6GAA6G;QAC7G,EAAE,EAAE,MAAM,CAAA;KACX,CAAA;CACF,CAAA;AAGD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,MAAM,EAAE;YACN,kBAAkB,EAAE,kBAAkB,CAAA;SACvC,CAAA;KACF;CACF;AAED,eAAO,MAAM,gBAAgB,GAAI,WAAW,KAAK,sBA6BhD,CAAA;AAED,eAAe,gBAAgB,CAAA"}
@@ -42,15 +42,23 @@ const react_1 = __importStar(require("react"));
42
42
  require("./theme.css");
43
43
  const ScalarDocusaurus = ({ route }) => {
44
44
  const ref = (0, react_1.useRef)(null);
45
+ const id = (0, react_1.useId)();
45
46
  (0, react_1.useEffect)(() => {
46
47
  if (!window.Scalar || !ref.current) {
47
48
  return;
48
49
  }
49
- // Create a new Scalar API Reference
50
- window.Scalar.createApiReference(ref.current, { ...route.configuration, hideDarkModeToggle: true });
51
- }, [ref]);
50
+ // Inject the init call as a real <script> so the browser parses the serialized configuration —
51
+ // including any functions — as live JavaScript. Passing it as a prop is not an option, because
52
+ // functions do not survive Docusaurus' JSON serialization of route props (see #6933).
53
+ const script = document.createElement('script');
54
+ script.textContent = `window.Scalar.createApiReference(document.getElementById(${JSON.stringify(id)}), ${route.configuration})`;
55
+ document.body.appendChild(script);
56
+ return () => {
57
+ script.remove();
58
+ };
59
+ }, [id, route.configuration]);
52
60
  return (react_1.default.createElement(Layout_1.default, null,
53
- react_1.default.createElement("div", { ref: ref })));
61
+ react_1.default.createElement("div", { id: id, ref: ref })));
54
62
  };
55
63
  exports.ScalarDocusaurus = ScalarDocusaurus;
56
64
  exports.default = exports.ScalarDocusaurus;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE5D,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,eAAe,CAAA;AAEjE,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,aAAa,CAAC,EAAE,4BAA4B,CAAA;CAC7C,CAAA;AAeD;;GAEG;AACH,QAAA,MAAM,gBAAgB,GACpB,SAAS,WAAW,EACpB,SAAS,aAAa,KACrB,MAAM,CAAC;IAAE,aAAa,CAAC,EAAE,4BAA4B,CAAA;CAAE,CAqDzD,CAAA;AAED,eAAe,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAG5D,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,eAAe,CAAA;AAEjE,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,aAAa,CAAC,EAAE,4BAA4B,CAAA;CAC7C,CAAA;AAeD;;GAEG;AACH,QAAA,MAAM,gBAAgB,GACpB,SAAS,WAAW,EACpB,SAAS,aAAa,KACrB,MAAM,CAAC;IAAE,aAAa,CAAC,EAAE,4BAA4B,CAAA;CAAE,CAoEzD,CAAA;AAED,eAAe,gBAAgB,CAAA"}
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const node_path_1 = __importDefault(require("node:path"));
7
7
  const utils_1 = require("@docusaurus/utils");
8
+ const client_side_rendering_1 = require("@scalar/client-side-rendering");
8
9
  /**
9
10
  * Used to set default options from the user-provided options
10
11
  * This is also useful to ensure backwards compatibility with older configs that don't have the new options
@@ -55,12 +56,22 @@ const ScalarDocusaurus = (context, options) => {
55
56
  position: 'left',
56
57
  });
57
58
  }
59
+ // Serialize the configuration to a JavaScript object literal here, in Node, while the config
60
+ // functions (onBeforeRequest, request hooks, sorters, …) are still live. Docusaurus JSON-serializes
61
+ // route props when it generates its routes module, which would otherwise silently drop every
62
+ // function-valued option. See https://github.com/scalar/scalar/issues/6933.
63
+ const givenConfiguration = content.configuration ?? {};
64
+ const configuration = Array.isArray(givenConfiguration) ? (givenConfiguration[0] ?? {}) : givenConfiguration;
65
+ // Normalize the configuration in Node first, exactly like the CDN HTML path does: this evaluates a
66
+ // function-valued `content` at build time (it must not be shipped to the browser) and reconciles
67
+ // `content` vs `url`. Only then do we serialize the remaining live functions for the browser.
68
+ const normalizedConfiguration = (0, client_side_rendering_1.getConfiguration)({ ...configuration, hideDarkModeToggle: true });
58
69
  // Add the appropriate route based on the module system
59
70
  addRoute({
60
71
  path: (0, utils_1.normalizeUrl)([baseUrl, defaultOptions.route ?? '/scalar']),
61
72
  component: node_path_1.default.resolve(__dirname, './ScalarDocusaurus'),
62
73
  exact: true,
63
- ...content,
74
+ configuration: (0, client_side_rendering_1.serializeConfigToJs)(normalizedConfiguration),
64
75
  });
65
76
  },
66
77
  };
package/package.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "testing",
20
20
  "react"
21
21
  ],
22
- "version": "0.8.27",
22
+ "version": "0.8.29",
23
23
  "engines": {
24
24
  "node": ">=22"
25
25
  },
@@ -48,7 +48,8 @@
48
48
  }
49
49
  },
50
50
  "dependencies": {
51
- "@scalar/types": "0.16.0"
51
+ "@scalar/client-side-rendering": "0.3.1",
52
+ "@scalar/types": "0.16.1"
52
53
  },
53
54
  "devDependencies": {
54
55
  "@docusaurus/core": "^3.9.2",