Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 94bee25

Browse files
committed
feat(Voyager): Add get full static resources from CDNs
1 parent 258067c commit 94bee25

File tree

11 files changed

+1319
-1766
lines changed

11 files changed

+1319
-1766
lines changed

voyager-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/voyager/boot/VoyagerController.java

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,73 @@
1919
* @author Guilherme Blanco
2020
*/
2121
@Controller
22-
public class VoyagerController
23-
{
22+
public class VoyagerController {
23+
24+
private static final String CDNJS_CLOUDFLARE_COM_AJAX_LIBS = "//cdnjs.cloudflare.com/ajax/libs/";
25+
private static final String CDN_JSDELIVR_NET_NPM = "//cdn.jsdelivr.net/npm/";
26+
private static final String FETCH = "fetch";
27+
private static final String ES_6_PROMISE = "es6-promise";
28+
private static final String REACT = "react";
29+
private static final String REACT_DOM = "react-dom";
30+
private static final String VOYAGER = "voyager";
31+
2432
@Value("${voyager.endpoint:/graphql}")
2533
private String graphqlEndpoint;
2634

2735
@Value("${voyager.pageTitle:Voyager}")
2836
private String pageTitle;
2937

38+
@Value("${voyager.static.basePath:/}")
39+
private String staticBasePath;
40+
3041
@Value("${voyager.cdn.enabled:false}")
3142
private Boolean voyagerCdnEnabled;
3243

33-
@Value("${voyager.cdn.version:v1.x}")
44+
@Value("${voyager.cdn.version:@1.0.0-rc.26}")
3445
private String voyagerCdnVersion;
3546

3647
@RequestMapping(value = "${voyager.mapping:/voyager}")
37-
public void voyager(HttpServletRequest request, HttpServletResponse response) throws IOException
38-
{
48+
public void voyager(HttpServletRequest request, HttpServletResponse response) throws IOException {
3949
String contextPath = request.getContextPath();
4050
response.setContentType("text/html; charset=UTF-8");
4151

42-
String voyagerCssUrl = contextPath + "/vendor/voyager.css";
43-
String voyagerJsUrl = contextPath + "/vendor/voyager.min.js";
44-
45-
if (voyagerCdnEnabled && StringUtils.isNotBlank(voyagerCdnVersion)) {
46-
voyagerCssUrl = "//apis.guru/graphql-voyager/releases/" + voyagerCdnVersion + "/voyager.css";
47-
voyagerJsUrl = "//apis.guru/graphql-voyager/releases/" + voyagerCdnVersion + "/voyager.min.js";
48-
}
49-
5052
String template = StreamUtils.copyToString(new ClassPathResource("voyager.html").getInputStream(), Charset.defaultCharset());
5153
Map<String, String> replacements = new HashMap<>();
5254
replacements.put("graphqlEndpoint", contextPath + graphqlEndpoint);
5355
replacements.put("pageTitle", pageTitle);
54-
replacements.put("voyagerCssUrl", voyagerCssUrl);
55-
replacements.put("voyagerJsUrl", voyagerJsUrl);
56+
replacements.put("es6PromiseJsUrl", getCdnJsUrl(staticBasePath, voyagerCdnEnabled, ES_6_PROMISE,
57+
"4.1.1", "es6-promise.auto.min.js"));
58+
replacements.put("fetchJsUrl", getCdnJsUrl(staticBasePath, voyagerCdnEnabled, FETCH,
59+
"3.0.0", "fetch.min.js"));
60+
replacements.put("reactJsUrl", getCdnJsUrl(staticBasePath, voyagerCdnEnabled, REACT,
61+
"16.8.3", "react.production.min.js"));
62+
replacements.put("reactDomJsUrl", getCdnJsUrl(staticBasePath, voyagerCdnEnabled, REACT_DOM,
63+
"16.8.3", "react-dom.production.min.js"));
64+
65+
replacements.put("voyagerCssUrl", getJsDeliverUrl(staticBasePath, voyagerCdnEnabled, VOYAGER,
66+
voyagerCdnVersion, "dist/voyager.css", "voyager.css"));
67+
replacements.put("voyagerJsUrl", getJsDeliverUrl(staticBasePath, voyagerCdnEnabled, VOYAGER,
68+
voyagerCdnVersion, "dist/voyager.min.js", "voyager.worker.min.js"));
69+
replacements.put("voyagerWorkerJsUrl", getJsDeliverUrl(staticBasePath, voyagerCdnEnabled, VOYAGER,
70+
voyagerCdnVersion, "dist/voyager.worker.min.js", "voyager.worker.min.js"));
5671
replacements.put("contextPath", contextPath);
5772

5873
response.getOutputStream().write(StrSubstitutor.replace(template, replacements).getBytes(Charset.defaultCharset()));
5974
}
75+
76+
private String getCdnJsUrl(String staticBasePath, Boolean isCdnEnabled, String library,
77+
String cdnVersion, String filename) {
78+
if (isCdnEnabled && StringUtils.isNotBlank(cdnVersion)) {
79+
return CDNJS_CLOUDFLARE_COM_AJAX_LIBS + library + cdnVersion + "/" + filename;
80+
}
81+
return staticBasePath + "vendor/" + filename;
82+
}
83+
84+
private String getJsDeliverUrl(String staticBasePath, Boolean isCdnEnabled, String library,
85+
String cdnVersion, String cdnFileName, String filename) {
86+
if (isCdnEnabled && StringUtils.isNotBlank(cdnVersion)) {
87+
return CDN_JSDELIVR_NET_NPM + library + "/" + cdnVersion + "/" + cdnFileName;
88+
}
89+
return staticBasePath + "vendor/" + filename;
90+
}
6091
}

voyager-spring-boot-autoconfigure/src/main/resources/static/vendor/es6-promise.auto.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

voyager-spring-boot-autoconfigure/src/main/resources/static/vendor/fetch.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

voyager-spring-boot-autoconfigure/src/main/resources/static/vendor/react-dom.min.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)