mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-04-19 11:11:18 +00:00
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.
This commit is contained in:
parent
39aaea5942
commit
964f948c64
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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 =
|
||||
|
@ -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) {
|
||||
|
@ -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 () {
|
||||
|
@ -24,7 +24,7 @@
|
||||
border-bottom-width: 1px;
|
||||
border-color: var(--md-nav-color-on-seperator)">
|
||||
<div class="container ">
|
||||
<a class="navbar-brand" th:href="@{'/'}" style="display: flex;">
|
||||
<a class="navbar-brand" th:href="${@contextPath}" style="display: flex;">
|
||||
<img class="main-icon" th:src="@{'/favicon.svg'}" alt="icon">
|
||||
<span class="icon-text" th:text="${@navBarText}"></span>
|
||||
</a>
|
||||
@ -265,17 +265,18 @@
|
||||
</div>
|
||||
</div>
|
||||
<script th:src="@{'/js/settings.js'}"></script>
|
||||
<script>
|
||||
<script th:inline="javascript">
|
||||
window.onload = function () {
|
||||
updateFavoritesDropdown();
|
||||
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const navbarLink = document.querySelector(".navbar-brand");
|
||||
const contentPath = /*[[${@contextPath}]]*/ '';
|
||||
if (localStorage.getItem("defaultView") === "home-legacy") {
|
||||
navbarLink.setAttribute("href", "/home-legacy");
|
||||
navbarLink.setAttribute("href", contentPath + "home-legacy");
|
||||
} else {
|
||||
navbarLink.setAttribute("href", "/");
|
||||
navbarLink.setAttribute("href", contentPath);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
@ -219,7 +219,7 @@
|
||||
window.analyticsPromptBoolean = /*[[${@analyticsPrompt}]]*/ false;
|
||||
/*]]>*/
|
||||
</script>
|
||||
<script th:src="@{'/js/pages/home.js'}"></script>
|
||||
<script th:src="@{'/js/pages/home.js'}" th:inline="javascript"></script>
|
||||
|
||||
|
||||
</body>
|
||||
|
@ -52,8 +52,8 @@
|
||||
const pagesTranslation = document.getElementById('pagesTranslation').innerText; // Get translation for multiple pages
|
||||
</script>
|
||||
<script type="module">
|
||||
import * as pdfjsLib from '/pdfjs-legacy/pdf.mjs';
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdfjs-legacy/pdf.worker.mjs';
|
||||
import * as pdfjsLib from './pdfjs-legacy/pdf.mjs';
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc = './pdfjs-legacy/pdf.worker.mjs';
|
||||
</script>
|
||||
<script th:src="@{'/js/merge.js'}"></script>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user