Open engrnand opened 7 months ago
What happens to the terminal when you access the documentation URL via cURL?
Change type
from "laravel" to "static" and double click the generated HTML.
If opening up the static HTML still crashes the page, whip up the Chrome DevTools and show us what comes up in the "network" and "console" tabs once you try to load the page from the file.
If the static-generated version of the documentation doesn't crash the page, do as suggested in previous paragraph in your current setup (type => "laravel"
) and show us what comes up in the terminal running the server as well.
I have change type from laravel to static, and delete all auto generated directories of scribe from view,storage,public direcory
after that I have run php artisan optimize:clear
command ,then I have run scribe generate command php -d memory_limit=1G artisan scribe:generate
. here is page data
Are you able to run php -d memory_limit=1G artisan scribe:generate
with scribe.php's config 'type' => 'static'
and share the contents of public/docs
with us?
Opening the 150_000-line index.html
file as is will consume about 1.2GB of RAM, which might throw Error code: out of memory
at the end user's machine's discretion.
Commenting out the following lines in the header of that file will make the page claim below 150MB of RAM, which is about half of what you'd expect from the YouTube home page.
<link rel="stylesheet" href="../docs/css/theme-default.style.css" media="screen">
<link rel="stylesheet" href="../docs/css/theme-default.print.css" media="print">
<link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.6.0/styles/obsidian.min.css">
<script src="https://unpkg.com/@highlightjs/cdn-assets@11.6.0/highlight.min.js"></script>
Hence, the inflated RAM usage comes from CSS styling.
If I remove the scripts, it removes all the styling of the web page. And Is this problem solvable?
Given that CSS styling is a front-end concern, the solution naturally belongs to the repository of the OpenAPI UI theme you're using.
However, I don't know where the default theme comes from.
Hence, here's a fix:
<script>
// Make .invisible class available globally
const invisibleStyle = `
.invisible {
display: none !important;
}
`;
const styleElement = document.createElement("style");
styleElement.textContent = invisibleStyle;
document.head.appendChild(styleElement);
// Display API sections conditionally to avoid CSS rendering overload
function handleHashChange() {
const targetCssSelector = ".page-wrapper > .content > *";
const sectionContainer = document.querySelectorAll(targetCssSelector);
sectionContainer.forEach(element => element.classList.add("invisible"))
// Get fragment identifier without the "#" symbol
const fragmentIdentifier = window.location.hash.substring(1);
// Get element matching the fragmentIdentifier in the URL or the first section
let elementToShow;
if (fragmentIdentifier) {
elementToShow = document.getElementById(fragmentIdentifier);
} else {
const firstH1 = document.querySelector(".page-wrapper > .content > h1");
if (firstH1) {
elementToShow = firstH1;
}
}
// Show the element matching the fragmentIdentifier (elementToShow)
if (elementToShow) {
elementToShow.classList.remove("invisible");
// Loop through the siblings of the elementToShow until another h1 tag is found
let nextElement = elementToShow.nextElementSibling;
while (nextElement && !nextElement.matches('h1')) {
nextElement.classList.remove("invisible");
nextElement = nextElement.nextElementSibling;
}
}
}
window.addEventListener("hashchange", handleHashChange);
document.addEventListener("DOMContentLoaded", handleHashChange);
</script>
Adding this right after the opening body tag (<body>
) of the static-generated index.html
will make the documentation page consume 450MB
of RAM instead of 1.2GB
of RAM.
As of today, I don't know the codebase well enough to make it work with scribe.php's config 'type' => 'laravel'
.
thanks, it works , I have create command for dynamically add script
public function handle()
{
ini_set('memory_limit', '1G');
Artisan::call("scribe:generate");
$output = Artisan::output();
echo $output;
if (config('scribe.type') == 'laravel') {
$indexFilePath =
$indexFilePath = resource_path('views/scribe/index.blade.php');
} else {
$indexFilePath = public_path('docs/index.html');
}
$indexContent = File::get($indexFilePath);
$scriptContent = '<script src="/uploads/scripts/scribe-custom.js"></script>';
// Find the position to insert the script content
$headEndPosition = strripos($indexContent, '</head>');
if ($headEndPosition !== false) {
$indexContent = substr_replace($indexContent, $scriptContent, $headEndPosition, 0);
} else {
// Handle case where </head> tag is not found
$this->error('</head> tag not found in index.blade.php. Script content was not inserted.');
return;
}
// Write the modified content back to index.blade.php
File::put($indexFilePath, $indexContent);
$this->info('Script content inserted successfully inside <head> tag of index.blade.php.');
}
Thanks for assisting @codespearhead !
Alternatively, can you try setting type
to external_static
or external_laravel
? (and also set the theme
value) This will make Scribe only generate an OpenAPI spec, and render client-side documentation from that spec.
for external_static
or external_laravel
need to create custom view, I think you guys need to fix it by default in the package
No, you didn't configure it correctly.
Your config should look like this:
'type' => 'external_laravel',
'theme' => 'elements',
facing error: when set above configration
facing error: when set above configration
This seems happen because in the elements component looks for an openapi.yaml file in ../docs/ but that file is not generated.
When I just hardcode the openapi spec link that is generated.
config/scribe.php
'type' => 'external_laravel', 'theme' => 'elements',
From scribe/index.blade.php
<elements-api apiDescriptionUrl="../docs/openapi.yaml" router="hash" layout="sidebar" hideTryIt="" logo="" />
To scribe/index.blade.php
<elements-api apiDescriptionUrl="/docs.openapi" router="hash" layout="sidebar" hideTryIt="" logo="" />
But this solution seems to break the CSRF protection with Try It Out.
When you don't use the external_laravel type ther seems to be a bug with the Highlight.js that freezes up the whole page, maybe that is the issue why it is not loading?
Commenting these out solves the issue of the incredible slow loading time, but at the expense that your Try It Out is not correctly styled.
scribe/index.blade.php
Oof. Maybe HighlightJS can't handle it. Is there no way to get the CSRF protection working? If possible, check out the documentation for elements/scalar/rapidoc. They might have some extra options you can pass with Scribe's config external.html_attributes
.
Hello,
Here a working solution :
'type' => 'external_laravel',
'theme' => 'elements',
'external' => [
'html_attributes' => [
'apiDescriptionUrl' => '/docs.openapi',
'basePath' => '/docs',
'layout' => 'responsive',
'router' => 'hash',
'tryItCredentialsPolicy' => 'same-origin',
]
],
I looked at the code, the issue is in this file : https://github.com/knuckleswtf/scribe/blob/master/src/Writing/HtmlWriter.php#L32
In laravel mode we have $this->assetPathPrefix set to '../docs/'
In the ExternalHtmlWriter we are appending 'openapi.yaml' so we have in finally
$metadata['openapi_spec_url'] = '../docs/openapi.yaml'
(https://github.com/knuckleswtf/scribe/blob/master/src/Writing/ExternalHtmlWriter.php#L36)
I'm not sure how to fix that without breaking other mode.
Best regards, Max
Scribe version
4
PHP version
8.3
Framework
Laravel
Framework version
10
Scribe config
What happened?
I'm facing an issue because I have a huge number of endpoints, and I also have multiple API route files. Initially, the docs were not generating successfully, but after increasing the memory limit, it started working. However, it is unable to load on the browser. After some time, it opens a waiting popup and then gets killed. I need a solution for this. Additionally, I have more than 200 module files, so I'm not creating individual config files for each. Therefore, I need a proper solution for this or something else that will fix it.
Docs