Skip to content

Commit

Permalink
feat(core): Add preliminary support for PJAX
Browse files Browse the repository at this point in the history
  • Loading branch information
PJ-568 committed Apr 25, 2024
1 parent 866c59f commit 5c5bb42
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
5 changes: 3 additions & 2 deletions layout/common/scripts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ module.exports = class extends Component {
{clipboard && <script src={cdn('clipboard', '2.0.4', 'dist/clipboard.min.js')} defer></script>}
<script dangerouslySetInnerHTML={{ __html: `moment.locale("${language}");` }}></script>
<script dangerouslySetInnerHTML={{ __html: embeddedConfig }}></script>
<script src={url_for('/js/column.js')}></script>
<script data-pjax src={url_for('/js/column.js')}></script>
<Plugins site={site} config={config} page={page} helper={helper} head={false} />
<script src={url_for('/js/main.js')} defer></script>
<script data-pjax src={url_for('/js/main.js')} defer></script>
<script src={url_for('/js/pjax.js')}></script>
</Fragment>;
}
};
2 changes: 1 addition & 1 deletion layout/plugin/back_to_top.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BackToTop extends Component {
<a id="back-to-top" title={title} href="javascript:;">
<i class="fas fa-chevron-up"></i>
</a>
<script src={jsUrl} defer></script>
<script data-pjax src={jsUrl} defer></script>
</Fragment>;

}
Expand Down
21 changes: 21 additions & 0 deletions layout/plugin/pjax.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { Component, Fragment } = require('inferno');
const { cacheComponent } = require('hexo-component-inferno/lib/util/cache');

class Pjax extends Component {
render() {
return <Fragment>
<script src={cdn('pjax', '0.2.8', 'min/pjax.min.js')}></script>
<script src={this.props.jsUrl}></script>
</Fragment>;
}
}

Pjax.Cacheable = cacheComponent(Pjax, 'plugin.Pjax', props => {
const { helper, head } = props;
return {
head,
jsUrl: helper.url_for('/js/pjax.js')
};
});

module.exports = Pjax;
36 changes: 36 additions & 0 deletions source/js/pjax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// eslint-disable-next-line no-unused-vars
let pjax;

function initPjax() {
try {
// eslint-disable-next-line no-undef
pjax = new Pjax({
selectors: [
'head title',
'.columns',
'.navbar-start',
'.navbar-end',
'.searchbox',
'#back-to-top',
'[data-pjax]',
'.pjax-reload'
]
});
} catch (e) {
console.warn('PJAX error: ' + e);
}
}

// // Listen for start of Pjax
// document.addEventListener('pjax:send', function() {
// return;
// // TODO pace start loading animation
// })

// // Listen for completion of Pjax
// document.addEventListener('pjax:complete', function() {
// return;
// // TODO pace stop loading animation
// })

window.addEventListener('DOMContentLoaded', () => initPjax());

0 comments on commit 5c5bb42

Please sign in to comment.