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

Added support for hosting from a subfolder #248

Merged
merged 3 commits into from
Jan 18, 2024

Conversation

jubalm
Copy link
Collaborator

@jubalm jubalm commented Jan 11, 2024

  • dynamically change the HTML base URL to the window's location
  • changed absolute / paths to relative path ./

Resolves #246

There may still be errors in the page before the base href was set but most modern browsers will find the files after the switch; at least on Chrome and Firefox was tested ok

Copy link

netlify bot commented Jan 11, 2024

Deploy Preview for kaleidoscopic-pudding-244d33 ready!

Name Link
🔨 Latest commit 632ffce
🔍 Latest deploy log https://app.netlify.com/sites/kaleidoscopic-pudding-244d33/deploys/65a8aec86fe53300081a8642
😎 Deploy Preview https://deploy-preview-248--kaleidoscopic-pudding-244d33.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@jubalm jubalm force-pushed the feature/ipfs-subpath-support branch from b0bc6a9 to fee1fb3 Compare January 11, 2024 04:27
Copy link
Contributor

@MicahZoltu MicahZoltu left a comment

Choose a reason for hiding this comment

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

Approved, but I would like more reasoning/discussion on the added script as it feels unnecessary.

app/index.html Outdated
Comment on lines 9 to 14
<!-- support hosting from a subpath such as ipfs -->
<script>
const baseElement = document.createElement('base')
baseElement.setAttribute('href', window.location.href.replace(/\/+$/, '') + '/')
document.head.appendChild(baseElement)
</script>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this necessary? Why not just include a <base href='.'> element in <head>? Does this not work and you have to include an explicit base path? That feels wrong to me. 🤔

If the document has no elements, then baseURI defaults to location.href

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Replaced location.href strategy of parsing subpath by using location.pathname

window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/'))

This commit enable support for hosting as /subpath/, /subpath/, /subpath/index.html and appropriately injects only when needed.

app/index.html Outdated

<!-- support hosting from a subpath such as ipfs -->
<script>
const subPath = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/'))
Copy link
Contributor

Choose a reason for hiding this comment

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

'/subfolder'.lastIndexOf('/') === 0

I don't think this will do what you want as it will strip the subfolder, but we want to keep it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const subPath = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/'))
const subPath = window.location.pathname.endsWith('index.html')
? window.location.pathname.substring(0, window.location.pathname.length - 'index.html'.length)
: window.location.pathname

I think something like this may work? Double check that a base href of '/subfolder' and '/subfolder/' work the same though, as this will not add a trailing slash if it is missing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just made it so that it will always remove index.html if it's in the path and just take the subpath from out of it by removing the trailing slash. will re-add it later as needed by the base element.

const subPath = window.location.pathname.replace('index.html', '').replace(/\/$/, '')
Screenshot 2024-01-18 at 12 50 40 PM

Copy link
Contributor

@MicahZoltu MicahZoltu left a comment

Choose a reason for hiding this comment

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

Looks good!

@jubalm jubalm merged commit f8cee3c into master Jan 18, 2024
4 checks passed
@jubalm jubalm deleted the feature/ipfs-subpath-support branch January 18, 2024 06:47
@jubalm jubalm linked an issue Jan 30, 2024 that may be closed by this pull request
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.

2 participants