1+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment' ;
2+
3+ export default ( function ( ) {
4+ if ( ! ExecutionEnvironment . canUseDOM ) {
5+ return null ; // Don't run on server-side
6+ }
7+
8+ return {
9+ onRouteUpdate ( { location} ) {
10+ // Check if the config is already set (e.g., by a previous navigation)
11+ // Also check if the script is added, maybe the config exists but script failed last time
12+ if ( ! window . difyChatbotConfig && ! document . getElementById ( 'wairWlveaRXYYqb5' ) ) {
13+ console . log ( 'Setting Dify config and adding script via client module' ) ;
14+
15+ // Define configuration
16+ window . difyChatbotConfig = {
17+ token : 'wairWlveaRXYYqb5' ,
18+ systemVariables : {
19+ // user_id: 'YOU CAN DEFINE USER ID HERE',
20+ // conversation_id: 'YOU CAN DEFINE CONVERSATION ID HERE, IT MUST BE A VALID UUID',
21+ } ,
22+ } ;
23+
24+ // Get CSP nonce from Docusaurus meta tag
25+ const nonceMeta = document . querySelector ( 'meta[name="docusaurus-csp-nonce"]' ) ;
26+ const nonceValue = nonceMeta ?. content ;
27+
28+ // Create and append the script tag
29+ const script = document . createElement ( 'script' ) ;
30+ script . src = 'https://udify.app/embed.min.js' ;
31+ script . id = 'wairWlveaRXYYqb5' ;
32+ if ( nonceValue ) {
33+ script . setAttribute ( 'nonce' , nonceValue ) ;
34+ console . log ( 'Applied nonce to Dify script:' , nonceValue ) ;
35+ }
36+ document . head . appendChild ( script ) ; // Append to head
37+ } else if ( window . difyChatbotConfig && ! document . getElementById ( 'wairWlveaRXYYqb5' ) ) {
38+ // Config exists, but script is missing (maybe removed or failed to load)
39+ console . log ( 'Dify config exists, re-adding script via client module' ) ;
40+
41+ // Get CSP nonce from Docusaurus meta tag
42+ const nonceMeta = document . querySelector ( 'meta[name="docusaurus-csp-nonce"]' ) ;
43+ const nonceValue = nonceMeta ?. content ;
44+
45+ const script = document . createElement ( 'script' ) ;
46+ script . src = 'https://udify.app/embed.min.js' ;
47+ script . id = 'wairWlveaRXYYqb5' ;
48+ if ( nonceValue ) {
49+ script . setAttribute ( 'nonce' , nonceValue ) ;
50+ console . log ( 'Applied nonce to Dify script (re-add):' , nonceValue ) ;
51+ }
52+ document . head . appendChild ( script ) ; // Append to head
53+ } else {
54+ console . log ( 'Dify config or script already present, skipping addition.' ) ;
55+ }
56+ } ,
57+ } ;
58+ } ) ( ) ;
0 commit comments