From 964f948c64e1d396e240b7aca59cf2366f10026c Mon Sep 17 00:00:00 2001 From: Ludy Date: Thu, 20 Mar 2025 08:53:29 +0100 Subject: [PATCH] Fix: Context Path Handling and Static Asset Loading Improvements (#3198) # Description of Changes ### Summary This PR improves how Stirling-PDF handles the `context-path` configuration in both backend and frontend components. It ensures proper URL generation when a custom `context-path` is set in the application properties. Additionally, it fixes static asset loading issues related to relative paths. ### Changes Implemented: - **Backend Enhancements:** - Introduced `contextPathStatic` as a static variable in `SPDFApplication.java` to store the configured `server.servlet.context-path`. - Modified log outputs and UI initialization URLs to include `contextPathStatic`. - Registered `contextPath` as a Spring Bean in `AppConfig.java` to make it accessible in templates. - **Frontend Fixes:** - Updated JavaScript files (`downloader.js`, `home.js`) to dynamically retrieve and use `contextPath`. - Adjusted Thymeleaf template files (`navbar.html`, `home.html`, `merge-pdfs.html`) to reference `contextPath` correctly. - Fixed incorrect static file paths (`pdf.worker.mjs`, `pdf.mjs`) by replacing absolute paths (`/`) with relative ones (`./`). ### Why These Changes? - Fixes issues where deployments under subpaths (e.g., `example.com/stirling-pdf/`) resulted in incorrect asset and navigation links. - Ensures compatibility with different deployment configurations where `context-path` is not `/`. ### Challenges Encountered - Ensuring all JavaScript and template references were correctly updated to use `contextPath` dynamically. - Maintaining backward compatibility for deployments that use `/` as the context path. Closes #3193 #3181 --- ## Checklist ### General - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [x] I have performed a self-review of my own code - [x] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details. --- .../stirling/software/SPDF/SPDFApplication.java | 13 +++++++++++-- .../stirling/software/SPDF/config/AppConfig.java | 6 ++++++ src/main/resources/static/js/downloader.js | 2 +- src/main/resources/static/js/pages/home.js | 3 ++- src/main/resources/templates/fragments/navbar.html | 9 +++++---- src/main/resources/templates/home.html | 2 +- src/main/resources/templates/merge-pdfs.html | 4 ++-- 7 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/SPDFApplication.java b/src/main/java/stirling/software/SPDF/SPDFApplication.java index 5ac9f663e..3cf89a657 100644 --- a/src/main/java/stirling/software/SPDF/SPDFApplication.java +++ b/src/main/java/stirling/software/SPDF/SPDFApplication.java @@ -37,6 +37,7 @@ public class SPDFApplication { private static String serverPortStatic; private static String baseUrlStatic; + private static String contextPathStatic; private final Environment env; private final ApplicationProperties applicationProperties; @@ -45,6 +46,9 @@ public class SPDFApplication { @Value("${baseUrl:http://localhost}") private String baseUrl; + @Value("${server.servlet.context-path:/}") + private String contextPath; + public SPDFApplication( Environment env, ApplicationProperties applicationProperties, @@ -138,7 +142,8 @@ public class SPDFApplication { @PostConstruct public void init() { baseUrlStatic = this.baseUrl; - String url = baseUrl + ":" + getStaticPort(); + contextPathStatic = this.contextPath; + String url = baseUrl + ":" + getStaticPort() + contextPath; if (webBrowser != null && Boolean.parseBoolean(System.getProperty("STIRLING_PDF_DESKTOP_UI", "false"))) { webBrowser.initWebUI(url); @@ -195,7 +200,7 @@ public class SPDFApplication { private static void printStartupLogs() { log.info("Stirling-PDF Started."); - String url = baseUrlStatic + ":" + getStaticPort(); + String url = baseUrlStatic + ":" + getStaticPort() + contextPathStatic; log.info("Navigate to {}", url); } @@ -220,4 +225,8 @@ public class SPDFApplication { public static String getStaticPort() { return serverPortStatic; } + + public static String getStaticContextPath() { + return contextPathStatic; + } } diff --git a/src/main/java/stirling/software/SPDF/config/AppConfig.java b/src/main/java/stirling/software/SPDF/config/AppConfig.java index 58719f1d0..46c580369 100644 --- a/src/main/java/stirling/software/SPDF/config/AppConfig.java +++ b/src/main/java/stirling/software/SPDF/config/AppConfig.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.Properties; import java.util.function.Predicate; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -78,6 +79,11 @@ public class AppConfig { return applicationProperties.getUi().getLanguages(); } + @Bean + public String contextPath(@Value("${server.servlet.context-path}") String contextPath) { + return contextPath; + } + @Bean(name = "navBarText") public String navBarText() { String defaultNavBar = diff --git a/src/main/resources/static/js/downloader.js b/src/main/resources/static/js/downloader.js index 77a673c04..900e2539a 100644 --- a/src/main/resources/static/js/downloader.js +++ b/src/main/resources/static/js/downloader.js @@ -130,7 +130,7 @@ async function getPDFPageCount(file) { try { const arrayBuffer = await file.arrayBuffer(); - pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdfjs-legacy/pdf.worker.mjs'; + pdfjsLib.GlobalWorkerOptions.workerSrc = './pdfjs-legacy/pdf.worker.mjs'; const pdf = await pdfjsLib.getDocument({data: arrayBuffer}).promise; return pdf.numPages; } catch (error) { diff --git a/src/main/resources/static/js/pages/home.js b/src/main/resources/static/js/pages/home.js index 6e72cdece..8565c44a2 100644 --- a/src/main/resources/static/js/pages/home.js +++ b/src/main/resources/static/js/pages/home.js @@ -33,10 +33,11 @@ function setAnalytics(enabled) { } updateFavoriteIcons(); +const contentPath = /*[[${@contextPath}]]*/ ''; const defaultView = localStorage.getItem('defaultView') || 'home'; // Default to "home" if (defaultView === 'home-legacy') { - window.location.href = '/home-legacy'; // Redirect to legacy view + window.location.href = contentPath + 'home-legacy'; // Redirect to legacy view } document.addEventListener('DOMContentLoaded', function () { diff --git a/src/main/resources/templates/fragments/navbar.html b/src/main/resources/templates/fragments/navbar.html index 8720b2f72..06defd47b 100644 --- a/src/main/resources/templates/fragments/navbar.html +++ b/src/main/resources/templates/fragments/navbar.html @@ -24,7 +24,7 @@ border-bottom-width: 1px; border-color: var(--md-nav-color-on-seperator)">
- + icon @@ -265,17 +265,18 @@
- diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index 1450c7489..925aefe8e 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -219,7 +219,7 @@ window.analyticsPromptBoolean = /*[[${@analyticsPrompt}]]*/ false; /*]]>*/ - + diff --git a/src/main/resources/templates/merge-pdfs.html b/src/main/resources/templates/merge-pdfs.html index f6ace6d74..d10b65801 100644 --- a/src/main/resources/templates/merge-pdfs.html +++ b/src/main/resources/templates/merge-pdfs.html @@ -52,8 +52,8 @@ const pagesTranslation = document.getElementById('pagesTranslation').innerText; // Get translation for multiple pages