-
Notifications
You must be signed in to change notification settings - Fork 430
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
Universal: ReferenceError: document is not defined #567
Comments
I think this issue can't be solved in this repo. It would be solved in dragula by checking something like |
Pending merge of bevacqua/dragula#534 |
Why isn't pull request merged? I need use dragula in angular-universal. Does anyone have a solution for this? Thank you |
Fixing the Edit: upgrading Dragula is unnecessary on the server side, so I am looking into a way to import |
I found a work-around that should help developers using Angular Universal. Create a set of stubbed out classes representing // dragula.directive.ts
import { Directive, Input } from '@angular/core';
/**
* this class stubs out ng2-dragula's DragulaDirective for loading server-side
* you don't need any of this functionality on the server
*/
@Directive({
/* tslint:disable */
// disable linting here, because you need the selector to match ng2-dragula's DragulaDirective
// which violates the selector prefix rules, if you have not disabled them
selector: '[dragula]'
/* tslint:enable */
})
export class DragulaDirective {
@Input() dragula: string;
@Input() dragulaModel: any;
@Input() dragulaOptions: any;
} // dragula.module.ts
import { NgModule } from '@angular/core';
import { DragulaDirective } from './dragula.directive';
import { DragulaService } from './dragula.service';
@NgModule({
declarations: [ DragulaDirective ],
exports: [ DragulaDirective ],
providers: [ DragulaService ],
})
class DragulaModule { }
export { DragulaDirective, DragulaModule, DragulaService }; // dragula.service.ts
import { Injectable, EventEmitter } from '@angular/core';
/**
* this class stubs out ng2-dragula's DragulaService for loading server-side
* you don't need any of this functionality on the server
*/
@Injectable()
export class DragulaService {
cancel: EventEmitter<any> = new EventEmitter();
cloned: EventEmitter<any> = new EventEmitter();
drag: EventEmitter<any> = new EventEmitter();
dragend: EventEmitter<any> = new EventEmitter();
drop: EventEmitter<any> = new EventEmitter();
out: EventEmitter<any> = new EventEmitter();
over: EventEmitter<any> = new EventEmitter();
remove: EventEmitter<any> = new EventEmitter();
shadow: EventEmitter<any> = new EventEmitter();
dropModel: EventEmitter<any> = new EventEmitter();
removeModel: EventEmitter<any> = new EventEmitter();
add(name: string, drake: any): any {}
find(name: string): any {}
destroy(name: string): void {}
setOptions(name: string, options: any): void {}
} Modify your "paths": {
"ng2-dragula": "path/to/dragula/stubs/dragula.module"
} When the SSR compiles, it will use this path to import the |
@asgallant - Sweet! Thanks. I managed to create those files, and get the build to work. Here's some issues and fixes I found (for my codebase), it might help others. In my
... but then I had to update
.. and actually it built fine w/o the change to |
Note that in v2 you might have to mock a few more items on those classes. But this seems good enough for now, pending merge upstream. |
@asgallant @jiggy1com I mocked everything out like in the example (replaced setOptions with createGroup) and used NormalModuleReplacementPlugin and everything builds just fine, but then when I actually try to load up my app the server side spits out this error |
got it working, took some small updates to those 3 files and then just adding the path in tsconfig.server.json rather than using NormalModuleReplacementPlugin in my webpack.server.config |
I'm almost there I guess.. but I still get one last error
EDIT: for future reference: I fixed the build. but it still uses the normal module when i try to start my server. So it fails..
|
I found another solutions requiring no stubbing at all but relies on domino in your server.ts:
|
@koraysels
My main.server.ts file below:
|
the code from your main.server.ts file should just be :
the other stuff belongs in the server.ts file in the root of your project. |
In our porject
But this doesn't change anything ;/ I use Angular 7.1.0 |
How can this be achieved in the latest version of Angular (17.2)? It does not have separate tsconfig for the SSR. |
Hi. angular universal does not have mock for document yet. How can I use it with server rendering enabled?
The text was updated successfully, but these errors were encountered: