Skip to content

Commit

Permalink
Fix scripts running when they should not (#217)
Browse files Browse the repository at this point in the history
* Make Swagger UI only load on the API page, fixes #215

* Make installer version requests only happen when required, fixes #216
  • Loading branch information
Pyrofab authored May 4, 2024
1 parent 87c465c commit 08ed1e4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@types/luxon": "^3.3.3",
"@types/prismjs": "^1.26.3",
"@types/semver-compare": "^1.0.3",
"@types/semver-sort": "^0.0.5",
"@types/swagger-ui": "^3.52.4",
"astro": "1.6.14",
"astro-i18next": "1.0.0-beta.12",
Expand Down
41 changes: 23 additions & 18 deletions src/components/parts/install/InstallerVersion.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ import { t } from "i18next";
---

<div class="field-label is-normal mt-1">
<span class="title is-6" id="installer-version"
>{t("install:installer-version-placeholder")}
</span>

<script>
import semverSort from "semver-sort";
<installer-version class="title is-6">
{t("install:installer-version-placeholder")}
</installer-version>
</div>

const VERSION_REGEX = /<version>(.+?)<\/version>/g;
<script>
import semverSort from "semver-sort";

const INSTALLER_URL_BASE =
"https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-installer/";
const MAVEN_METADATA_URL = INSTALLER_URL_BASE + "maven-metadata.xml";
const VERSION_REGEX = /<version>(.+?)<\/version>/g;
const INSTALLER_URL_BASE =
"https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-installer/";
const MAVEN_METADATA_URL = INSTALLER_URL_BASE + "maven-metadata.xml";

const metadataRequest = await fetch(MAVEN_METADATA_URL, { headers: { "User-Agent": "QuiltMC Website API" } })
const metadata = await metadataRequest.text()
const allVersion = Array.from(metadata.matchAll(VERSION_REGEX)).map(match => match[1])
const latest = semverSort.desc(allVersion)[0]
class InstallerVersion extends HTMLElement {
constructor() {
super();
}

if (document.getElementById("installer-version")) {
document.getElementById("installer-version").textContent = latest;
async connectedCallback() {
const metadataRequest = await fetch(MAVEN_METADATA_URL, { headers: { "User-Agent": "QuiltMC Website API" } });
const metadata = await metadataRequest.text();
const allVersion = Array.from(metadata.matchAll(VERSION_REGEX)).map(match => match[1]);
this.textContent = semverSort.desc(allVersion)[0]; // latest
}
</script>
</div>
}

customElements.define('installer-version', InstallerVersion);
</script>
19 changes: 10 additions & 9 deletions src/pages/api.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import swaggerDarkCss from "../styles/swagger-dark.css?raw";
---

<script>
import SwaggerUI from 'swagger-ui';

window.addEventListener("load", () => {
window.ui = SwaggerUI({
url: '/api/openapi.yaml',
dom_id: '#swagger-ui',
deepLinking: true,
});
});
const swaggerUiNode = document.getElementById('swagger-ui');
if (swaggerUiNode) {
import('swagger-ui').then(({ default: renderSwagger }) => {
window.ui = renderSwagger({
url: '/api/openapi.yaml',
domNode: swaggerUiNode,
deepLinking: true,
});
});
}
</script>
<style is:inline>
pre.version {
Expand Down

1 comment on commit 08ed1e4

@Cozy-GitHub
Copy link

Choose a reason for hiding this comment

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

See preview on Cloudflare Pages: https://ffae5a83.quiltmc-org.pages.dev

Please sign in to comment.