-
Notifications
You must be signed in to change notification settings - Fork 132
[FIX]: added fallback page to prevent white HTML page #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vatsal-Verma
wants to merge
11
commits into
accordproject:main
Choose a base branch
from
Vatsal-Verma:vatsal/444/fallback-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−2
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d977775
added fallback page to prevent white page error
Vatsal-Verma 359725c
Update scripts/fix-empty-pages.js
Vatsal-Verma e7301c5
Update scripts/fix-empty-pages.js
Vatsal-Verma 8b66fa1
Update views/fallback.njk
Vatsal-Verma a006add
Update views/fallback.njk
Vatsal-Verma 3988abb
Merge branch 'main' into vatsal/444/fallback-page
Vatsal-Verma 9702e39
Update fix-empty-pages.js
Vatsal-Verma fab43ca
Update fix-empty-pages.js
Vatsal-Verma f05e8d3
Update fallback.njk
Vatsal-Verma b6b5dd8
Update fix-empty-pages.js
Vatsal-Verma 96d1da3
Update fix-empty-pages.js
Vatsal-Verma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Script to scan `build/*.html` and replace empty pages with the `fallback.njk` rendering. | ||
| * Usage: node scripts/fix-empty-pages.js | ||
| */ | ||
|
|
||
| const fs = require('fs-extra'); | ||
| const path = require('path'); | ||
| const nunjucks = require('nunjucks'); | ||
| const jsdom = require('jsdom'); | ||
|
|
||
| nunjucks.configure(path.resolve(__dirname, '../views'), { autoescape: false }); | ||
|
|
||
| const buildDir = path.resolve(__dirname, '../build'); | ||
| const templateIndexPath = path.join(buildDir, 'template-library.json'); | ||
| const serverRoot = process.env.SERVER_ROOT ? process.env.SERVER_ROOT : 'https://templates.accordproject.org'; | ||
| const studioRoot = 'https://studio.accordproject.org'; | ||
| const githubRoot = `https://github.dev/accordproject/cicero-template-library/blob/master`; | ||
|
|
||
| async function run() { | ||
| if (!(await fs.pathExists(buildDir))) { | ||
| throw new Error(`Build directory "${buildDir}" does not exist. Please run the build (e.g., "npm run build") before running this script.`); | ||
| } | ||
| const files = (await fs.readdir(buildDir)).filter(f => f.endsWith('.html')); | ||
|
|
||
| let templateIndex = {}; | ||
| try { | ||
| if(await fs.pathExists(templateIndexPath)) { | ||
| templateIndex = await fs.readJson(templateIndexPath); | ||
| } | ||
| } catch(e) { | ||
| console.warn('Could not read template index:', e); | ||
| } | ||
|
|
||
| for(const f of files) { | ||
| if(f === 'index.html') continue; | ||
| const filePath = path.join(buildDir, f); | ||
| let content; | ||
| try { | ||
| content = await fs.readFile(filePath, 'utf8'); | ||
| } catch(e) { | ||
| console.error(`Failed reading ${filePath}:`, e); | ||
| continue; | ||
| } | ||
|
|
||
Vatsal-Verma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Cheap pre-check: if the <body> clearly contains non-whitespace content, | ||
| // skip the expensive JSDOM parsing for this file. | ||
| const bodyMatch = content.match(/<body[^>]*>([\s\S]*?)<\/body>/i); | ||
| if (bodyMatch && bodyMatch[1] && bodyMatch[1].trim()) { | ||
| // Body is clearly non-empty; no need to process further. | ||
| continue; | ||
| } | ||
|
|
||
| // Fallback to JSDOM for files that might be empty or are uncertain. | ||
| const dom = new jsdom.JSDOM(content); | ||
| const bodyContent = dom.window.document && dom.window.document.body ? dom.window.document.body.innerHTML.trim() : ''; | ||
| if(!bodyContent) { | ||
| const identifier = path.basename(f, '.html'); | ||
| console.log(`Empty page detected: ${f} (identifier: ${identifier}). Replacing with fallback.`); | ||
|
|
||
| const meta = templateIndex[identifier] || {}; | ||
| const displayName = meta.displayName || identifier; | ||
| const description = meta.description || ''; | ||
| const archiveURL = `./archives/${identifier}.cta`; | ||
| const studioURL = `${studioRoot}/?template=${encodeURIComponent('ap://' + identifier + '#hash')}`; | ||
| const githubURL = `${githubRoot}/src/${encodeURIComponent(identifier.split('@')[0])}/README.md`; | ||
|
|
||
| const fallbackHtml = nunjucks.render('fallback.njk', { | ||
| serverRoot: serverRoot, | ||
| filePath: f, | ||
| identifier: identifier, | ||
| displayName: displayName, | ||
| description: description, | ||
| studioURL: studioURL, | ||
| githubURL: githubURL, | ||
| archiveURL: archiveURL, | ||
| umlCardURL: `${serverRoot}/assets/images/A-MARK-ACCORDPROJECT-ONELINE-white.svg` | ||
| }); | ||
Vatsal-Verma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| await fs.writeFile(filePath, fallbackHtml); | ||
| console.log(`Replaced ${f} with fallback page.`); | ||
| } catch(e) { | ||
| console.error(`Failed writing ${filePath}:`, e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| run().catch(e => { | ||
| console.error('Script failed:', e); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| {% extends "page.njk" %} | ||
|
|
||
| {% block unfurl %} | ||
| <!-- Open Graph / Twitter cards for sharing --> | ||
| <meta property="og:url" content="{{serverRoot}}/{{filePath}}" /> | ||
| <meta property="og:title" content="{{displayName}}" /> | ||
| <meta property="og:description" content="{{description}}" /> | ||
| <meta property="og:image" content="{{umlCardURL}}" /> | ||
|
|
||
| <meta name="twitter:title" content="{{displayName}}" /> | ||
| <meta name="twitter:description" content="{{description}}" /> | ||
| <meta name="twitter:image" content="{{umlCardURL}}" /> | ||
| <meta name="twitter:url" content="{{serverRoot}}/{{filePath}}" /> | ||
| {% endblock %} | ||
|
|
||
| {% block body %} | ||
| <section class="section inner-content"> | ||
| <div class="container"> | ||
| <h1 class="title is-1">{{displayName}}</h1> | ||
|
|
||
| <div class="tags"> | ||
| <span class="tag is-light">{{identifier}}</span> | ||
| <span class="tag is-light">Fallback</span> | ||
| </div> | ||
|
|
||
| <p class="subtitle"> | ||
| {% if description %}{{description}}{% else %}<em>No description available.</em>{% endif %} | ||
| </p> | ||
|
|
||
| <div class="buttons"> | ||
| <a href="{{archiveURL}}" class="button is-rounded is-primary">Download Archive</a> | ||
| <a href="{{studioURL}}" class="button is-rounded is-primary">Open in Template Studio</a> | ||
| <a href="{{githubURL}}" class="button is-rounded is-primary">Open in VSCode Web</a> | ||
| </div> | ||
|
|
||
| <hr/> | ||
|
|
||
| <div class="content box"> | ||
| <p> | ||
| This page could not be generated automatically. The repository likely lacks assets for this template. | ||
| </p> | ||
| <p> | ||
| We replaced the empty output with this fallback so users can still see the template metadata and links to the archive and source. | ||
| </p> | ||
| </div> | ||
|
|
||
| <div class="columns"> | ||
| <div class="column is-half"> | ||
| <h4 class="title is-4">Template Identifier</h4> | ||
| <pre><code>{{identifier}}</code></pre> | ||
| </div> | ||
| <div class="column is-half"> | ||
| <h4 class="title is-4">Links</h4> | ||
| <ul> | ||
| <li><a href="{{archiveURL}}">Archive (.cta)</a></li> | ||
| <li><a href="{{studioURL}}">Open in Template Studio</a></li> | ||
| <li><a href="{{githubURL}}">Open README in VSCode Web</a></li> | ||
| </ul> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="content has-text-centered"> | ||
| <img src="{{umlCardURL}}" alt="UML diagram placeholder" style="max-width:240px; opacity:0.9;"/> | ||
| </div> | ||
|
|
||
| <h2 class="title is-2">Technical Integration</h2> | ||
| <div class="content"> | ||
| <p> | ||
| This is a fallback page — for the template source and code, check the README on GitHub: <a href="{{githubURL}}">{{githubURL}}</a> | ||
| </p> | ||
| </div> | ||
| </div> | ||
|
|
||
| </section> | ||
| {% endblock %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.