Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed build for usage without subdomain #806

Merged
merged 5 commits into from
Nov 10, 2020
Merged

Conversation

sattellite
Copy link
Contributor

I installed remark42 in the easiest way from the docker. Configured nginx to use app without subdomain. It works okay for comments but widgets not working as expected. Widgets use it own config with host as window.location.origin which returns the fqdn only.

This can be fixed on server side by multiple sed commands in container like this docker exec -it remark42 sed -i "s|host: window.location.origin|host: 'https://b.sattellite.me/comments/'|g" /srv/web/last-comments.html

I prepared fix to code for exclude this problem in future:

  • Replaced host value from window.location.origin to "REMARK_URL" || window.location.origin.
  • Changed init script for container to replace forced site_id by 'remark' to environment SITE_ID.


if [ -n "${SITE_ID}" ]; then
#replace "site_id: 'remark'" by SITE_ID
se -i "s|'remark'|'${SITE_ID}'|g" /srv/web/*.html
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, I think SITE_ID could be passed via env vars on build time without replacing it after.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if I will build my own docker image this pull request not needed. But I use public docker imageumputun/remark42 and this changes will fix it for usage without subdomain.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Replacing remark could cause side-effects like replacing this word in unexpected palaces.
I could suggest putting unique value or template like expression to places that we want to replace.

Something like {% SITE_ID %} and comment about how it works.
The same we should do for replacing REMARK_URL

Copy link
Contributor Author

@sattellite sattellite Nov 9, 2020

Choose a reason for hiding this comment

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

Yes, current solution is dirty hack. And site_id is hard coded. But this script called only inside docker container. Would not this solution break the usage without docker on bare metal?

If I will add expressions like {% SITE_ID %} and {% REMARK_URL %} it will keep the same after npm run build. And replace expressions to correct values will require postbuild script with sed commands.

I don't know how to fix this correctly, given what I said above.
Or am I wrong?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep, you are right. And now we have the issue in bin build. With this PR we will fix the issue only for docker.
Let's use your fix and I will think about how to fix it in all ways for all builds.

@umputun
Copy link
Owner

umputun commented Nov 2, 2020

confused. How this even related to subdomain configuration vs without subdomain configuration? Either way REMARK_URL has to be passed in or UI it won't be able to communicate with the server.

Widgets use it own config with host as window.location.origin which returns the fqdn only.

I don't get it. @akellbl4 correct if I wrong, but I think window.location.origin is not used to discover the server. I don't see how this can be the case and how window.location.origin can be used as a substitution for REMARK_URL

@akellbl4
Copy link
Collaborator

akellbl4 commented Nov 2, 2020

@umputun, I forgot to say that I made review only from code point of view. And I want to reproduce this bug and check how this PR will fix it.
window.location.origin takes current host name where document is served. It means this variable should represent current remark host.

@sattellite
Copy link
Contributor Author

sattellite commented Nov 2, 2020

This fix affects the pages of widgets, when directly accessing them.

/srv/web contains static html files for widgets: comments.html, last-comments.html, counter.html and demo page index.html.
This pages contain mostly identical inline js-config for remark42:

var remark_config = {
    site_id: 'remark',
    host: window.location.origin,
    components: ['last-comments'] // or other 
};

And this config is used to append widget scripts:

(function(c) {
  for (var i = 0; i < c.length; i++) {
    var d = document,
      s = d.createElement('script');
    s.src = remark_config.host + '/web/' + c[i] + '.js';
    (d.head || d.body).appendChild(s);
  }
})(remark_config.components || ['embed']);

remark_config.host contains origin (fqdn) instead of correct REMARK_URL. Generates incorrect url for component => script not loaded => widget don't working.

Current behavior for REMARK_URL=https://remark.example.com:

Current behavior for REMARK_URL=https://example.com/remark:

My patches force to set correct REMARK_URL and SITE_ID to relevant variables:

var remark_config = {
    site_id: 'mysiteid',
    host: 'https://example.com/remark' || window.location.origin,
    components: ['last-comments'] // or other 
};

And sed is needed to replace constant values that was used when building docker image.

@umputun
Copy link
Owner

umputun commented Nov 2, 2020

thx for the detailed explanation.

sed we currently doing is not something I proud of. If possible it will be nice to resolve the issue without adding more seds.

@sattellite
Copy link
Contributor Author

sed is simple solution in my opinion. Other solutions make it more difficult:

  • on backend side serve as templates, not static files. Fully generate all html and js with webpack to correct files for html/template. All sed will not be needed.
  • on backend side add api route to get current host and bucket (no new sed, old sed will remain)
  • on web server(nginx, caddy, etc.) side replace strings. Ugly and difficult, but sed will be not needed.

@sattellite
Copy link
Contributor Author

And if create serving templates instead static files it will be step to solve style customization issue #5

@akellbl4
Copy link
Collaborator

akellbl4 commented Nov 3, 2020

@sattellite as I said in comments all of these things could be done only with passing env vars via webpack to templates and js.
Also as I understood it's not a critical bug because it doesn't work only on the demo page.

UPD: I've checked our building process again. And if we want to handle it via webpack we need to do it on the build stage and variables pass to container on run time. It means we could change these vars only by sed or be serving them and replace it by go.

@akellbl4
Copy link
Collaborator

akellbl4 commented Nov 3, 2020

This bug relative to the PR.

@sattellite
Copy link
Contributor Author

sattellite commented Nov 3, 2020

UPD: I've checked our building process again. And if we want to handle it via webpack we need to do it on the build stage and variables pass to container on run time. It means we could change these vars only by sed or be serving them and replace it by go.

Agree with you. Serving with go web-server also can help solve other problems, as I said upper.

Does this mean that I need to add patches to process this with go?

@akellbl4
Copy link
Collaborator

akellbl4 commented Nov 4, 2020

@umputun wdyt? Using sed means we have some bad practice but an easier way to serve HTML. Serve templates with transformation in go means cleaner code but more complexity in this place.

I prefer to stay with sed for now and think about how we can handle it in some other way because I don't want to bring more complexity.

@umputun
Copy link
Owner

umputun commented Nov 6, 2020

yeah, agree. will keep sed for now

@@ -180,21 +180,33 @@ module.exports = () => ({
new Html({
template: path.resolve(__dirname, 'index.ejs'),
inject: false,
templateParameters: {
Copy link
Collaborator

@akellbl4 akellbl4 Nov 8, 2020

Choose a reason for hiding this comment

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

Sorry for mixing you up. This way is absolutely useless for us and we can remove these params.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This option allows use REMARK_URL instead of htmlWebpackPlugin.options.REMARK_URL

https://github.com/jantimon/html-webpack-plugin#:~:text=templateParameters

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep, I understand. But it's useless because we should to replace strings only on running time with passed vars to env and this template compilation will be done on building stage.

@@ -25,7 +25,7 @@
<script>
var remark_config = {
site_id: 'remark',
host: window.location.origin,
host: '<%= REMARK_URL %>' || window.location.origin,
Copy link
Collaborator

Choose a reason for hiding this comment

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

REMARK_URL is required param. It means this expression will always return the first value and we don't need to keep window.location.origin

Copy link
Owner

@umputun umputun left a comment

Choose a reason for hiding this comment

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

thx

@umputun umputun merged commit 60f554f into umputun:master Nov 10, 2020
umputun pushed a commit that referenced this pull request Nov 10, 2020
REMARK_URL is required and window.location.origin is not needed.

#806 (comment)
@paskal paskal added this to the v1.7 milestone Jan 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants