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:
Ludy
2025-03-20 08:53:29 +01:00
committed by GitHub
parent 39aaea5942
commit 964f948c64
7 changed files with 28 additions and 11 deletions

View File

@@ -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;
}
}

View File

@@ -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 =